regis

mail@pastecode.io avatar
unknown
javascript
3 years ago
3.5 kB
7
Indexable
Never
import fetch from 'node-fetch';
import {URLSearchParams} from 'url';
import readlineSync from 'readline-sync';
import { v4 as uuidv4 } from 'uuid';
import { GraphQLClient, gql } from 'graphql-request'

const functionGetTokenReg = (email, pass) => new Promise((resolve, reject) => {
    const urlapi = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=AIzaSyCX35qaNrESINLfA4qwfqPQb6cNHnEzAMs';
    const params = new URLSearchParams;
    params.append('email', email);
    params.append('password', pass);
    params.append('returnSecureToken', true);

    fetch(urlapi, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'x-client-version': 'ReactNative/JsCore/7.7.0/FirebaseCore-web',
            'Host': 'www.googleapis.com',
            'content-length': 938,
            'Accept-Encoding': 'gzip',
            'user-agent': 'okhttp/3.12.1',
        },
        body: params

    })
    .then( res => res.json())
    .then( result => resolve(result))
    .catch(err => reject(err))
});
const functionAccInfo = (token) => new Promise((resolve, reject) => {
    const urlapi = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=AIzaSyCX35qaNrESINLfA4qwfqPQb6cNHnEzAMs';
    const params = new URLSearchParams;
    params.append('idToken', token);

    fetch(urlapi, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'x-client-version': 'ReactNative/JsCore/7.7.0/FirebaseCore-web',
            'Host': 'www.googleapis.com',
            'content-length': 938,
            'Accept-Encoding': 'gzip',
            'user-agent': 'okhttp/3.12.1',
        },
        body: params

    })
    .then( res => res.json())
    .then( result => resolve(result))
    .catch(err => reject(err))
});


(async () => {
    try{
        const email = "digitalprem122@etlgr.com";
        const pass = "test123";
        
        const tokenREG = await functionGetTokenReg(email,pass)
        console.log(tokenREG.idToken)
        const GetAccInfo = await functionAccInfo(tokenREG.idToken)
        console.log(GetAccInfo)
        
        const endpoint = 'https://prod.pp-app-api.com/v1/graphql'
        const graphQLClient = new GraphQLClient(endpoint, {
            headers: {
              authorization: `Bearer ${tokenREG.idToken}`,
              'Host':'prod.pp-app-api.com',
              'Content-Type':'application/json',
              'content-length':380,
              'accept-encoding':'gzip',
              'user-agent':'okhttp/3.12.1'
            },
          })
        const qRegist = gql`
         mutation insert_multiple_users($objects: [users_insert_input!]!) {
             insert_users(objects: $objects) {
                 returning {
                     id
                     __typename
                    }
                    __typename
                }
            }
         `
         const variablesRegis = {
            objects: {
                "email": email,
                "first_name": "dhani",
                "last_name": "hokiis",
                "last_logged_in": GetAccInfo.users[0].lastRefreshAt 
              }
         }
         const registAcc = await graphQLClient.request(qRegist, variablesRegis)
         console.log(registAcc)



    }catch(e){
        console.log(e)
    }
})();