123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- // practises
- // let str = "asdasdadsdsaads";
- // function checkSum(str) {
- // let result = 0;
- // for (let letter of str) {
- // result += letter.charCodeAt();
- // }
- // return result;
- // }
- // console.log(checkSum(str));
- // function sign(data, salt) {
- // const json = JSON.stringify(data);
- // const sum = checkSum(json + salt);
- // return json + sum;
- // }
- // console.log(sign({ id: 100500 }, "FBSALT"));
- // function verify(signed, salt) {
- // const json = signed.slice(0, signed.lastIndexOf("}") + 1);
- // const tokenSum = +signed.slice(signed.lastIndexOf("}") + 1);
- // const sum = checkSum(json + salt);
- // console.log(json, tokenSum, sum);
- // return sum === tokenSum;
- // }
- // console.log(verify('{"id":100500}1317', "FBSALT"));
- // homework
- const backURL = "http://shop-roles.asmer.fs.a-level.com.ua";
- localStorage.authToken =
- "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOnsiaWQiOiI2MWE0ZGIwZWM3NTBjMTJiYTZiYTQwMWYiLCJsb2dpbiI6ImFib2JhIiwiYWNsIjpbIjYxYTRkYjBlYzc1MGMxMmJhNmJhNDAxZiIsInVzZXIiXX0sImlhdCI6MTYzODE5NTk1Mn0.qyOhmkFMZk1Xqx-cx8FyI2LiWbYgJyPZLOzk4Ng_zDY";
- const getGQL = (url) => (query, variables) =>
- fetch(url, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- ...(localStorage.authToken
- ? { Authorization: "Bearer " + localStorage.authToken }
- : {}),
- },
- body: JSON.stringify({ query, variables }),
- })
- .then((res) => res.json())
- .then((data) => {
- if ("errors" in data) {
- console.log(data.errors);
- }
- return data.data;
- });
- const gql = getGQL(`${backURL}/graphql`);
- async function registration() {
- return await gql(
- `mutation reg($login:String, $password:String){
- UserUpsert(user:{login:$login, password:$password, nick:$login}){
- _id login
- }
- }`,
- { login: "asdasd939", password: "123456" }
- );
- }
- async function login() {
- return await gql(
- `query log($login:String, $password:String){
- login(login:$login, password: $password)
- }`,
- { login: "aboba", password: "123456" }
- );
- }
- async function getOrder() {
- return await gql(
- `query o($q:String){
- OrderFind(query:$q){
- _id total orderGoods{
- price count total
- good{
- name categories{
- name
- }
- }
- }
- }
- }`,
- { q: "[{}]" }
- );
- }
- async function showData() {
- const reg = await registration();
- const log = await login();
- const order = await getOrder();
- console.log(reg);
- console.log(log);
- console.log(order);
- }
- showData();
|