Browse Source

GraphQL auth

Vladislav342 2 years ago
parent
commit
c281469ac9
1 changed files with 117 additions and 0 deletions
  1. 117 0
      HW_16/index.html

+ 117 - 0
HW_16/index.html

@@ -0,0 +1,117 @@
+<!DOCTYPE html>
+<html>
+<head>
+	<meta charset="utf-8">
+	<title></title>
+</head>
+<body>
+
+	<script>
+	/*const originalFetch = fetch;
+	fetch = (url, params={headers:{}}) => { 
+    params.headers.Authorization = "Bearer " + localStorage.authToken
+    	return originalFetch(url, params)
+	}*/
+
+	localStorage.authToken="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOnsiaWQiOiI2MWE0ZTA1MmM3NTBjMTJiYTZiYTQwMjkiLCJsb2dpbiI6InZsYWRCcmF1bjQiLCJhY2wiOlsiNjFhNGUwNTJjNzUwYzEyYmE2YmE0MDI5IiwidXNlciJdfSwiaWF0IjoxNjM5MzgwOTIzfQ.DLQ5Ei2bjg4i8eUXkpxANEiDY3P9khMdNmcElWioZ20";
+
+	const gql=(url,query, variables)=>fetch(url, {
+		method:"POST",
+		headers:{
+			'content-type':'application/json',
+			"Authorization":"Bearer "+localStorage.authToken
+		}, 
+		body: JSON.stringify({query,variables})
+	})
+	.then(res=>res.json())
+
+	function catById(_id){
+		 (async () => {
+		  let res = await gql("http://shop-roles.asmer.fs.a-level.com.ua/graphql",`query catById($query:String){
+		    CategoryFindOne(query:$query){
+		      name goods{
+		        _id name
+		      }
+		    }
+		}`, {query: JSON.stringify([{_id}])})
+		  console.log(res)
+		 })();
+	}
+	catById("5dc458985df9d670df48cc47")
+
+	function auth(login, password) {
+	    (async () => {
+			let res = await gql("http://shop-roles.asmer.fs.a-level.com.ua/graphql",`query log($login: String, $password: String) {
+		    	login(login: $login, password: $password)
+			}`, {login: login, password: password})
+			console.log(res)
+	    })()
+	}
+	auth("vladBraun5", "1234");
+	auth("vladBraun4","123");
+
+	/*function rootCats() {
+		(async () => {
+		  let res = await gql("http://shop-roles.asmer.fs.a-level.com.ua/graphql",`query {
+		        CategoryFind(query: $query){
+		            name goods{
+		            	_id name
+		            }
+		        }
+		    }`)
+			console.log(res);
+		})();
+	}
+	rootCats()*/
+
+	function reg(login, password) {
+	  (async () => {
+		 let res = await gql("http://shop-roles.asmer.fs.a-level.com.ua/graphql",`mutation reg($login: String, $password: String){
+		  UserUpsert(user: {login: $login, 
+		    				password: $password, 
+		            nick: $login}){
+		            _id login
+		            }
+		}`, {login: login, password: password})
+		console.log(res)
+	  })();
+	}
+
+	function goodFind() {
+		(async () => {
+		  	let res = await gql("http://shop-roles.asmer.fs.a-level.com.ua/graphql",`query goodz {
+			  GoodFind(query: "[{}]") {
+			    _id
+			    name
+			    price
+			    categories {
+			      _id
+			      name
+			    }
+			  }
+			}`)
+		    console.log(res)
+		})();
+	}
+	goodFind()
+
+	/*function newOrder(count, _id) {
+	  (async () => {
+		let res = await gql("http://shop-roles.asmer.fs.a-level.com.ua/graphql",`mutation newOrder{
+			   	OrderUpsert(order:{
+			        orderGoods:[
+			        	{count: $count, good: {_id: $id}}
+			      	]
+			    })
+		    {
+		      _id total 
+		    }`, {count: count, id: _id})
+		    console.log(res)
+	  	})();
+	}
+	
+	newOrder(3,"5dc8885e0e36db246e3049c4");*/
+
+	</script>
+</body>
+</html>