Skip to content

Commit

Permalink
modified client for social auth
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushik94 committed Apr 16, 2022
1 parent dda4a19 commit ac41b2b
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 39 deletions.
103 changes: 103 additions & 0 deletions RocketsAuth.js
@@ -0,0 +1,103 @@

import axios from "axios";
import Cookies from 'js-cookie';

axios.defaults.withCredentials = true;

(async () => {
if (window.location.href == Cookies.get('githubRedirectUrl')) {
const cookies = await axios.get(Cookies.get("api_url")+ "/tokens");
const { access, refresh } = cookies.data;
Cookies.set("jwt", access, { expires: 7 });
Cookies.set("refresh", refresh, { expires: 7 });
}
})().catch(err => {
console.error(err);
});


export default class Auth {
constructor(config) {
const { baseURL } = config;
this.baseURL = baseURL;
}
async login ({email, password, provider} = {}) {
if (provider == "local") {
const login = await axios.post(this.baseURL+"/signin", {
email: email,
password: password,
})
const {jwt, refresh} = login.data;
Cookies.set("jwt", jwt, { expires: 7 });
Cookies.set("refresh", refresh, { expires: 7 });
return true;
}
}
async signIn ({email, password, provider} = {}) {
switch (provider) {
case "github":
// first get the redirect URL
// and provider URL
console.log("getting github client test", this.baseURL);
const client = await axios.get(this.baseURL+"/github/client")
console.log("client: ", client)
const {ProviderUrl, RedirectUrl} = client.data;
// create a listener to update
// cookies when redirectURL is reached
Cookies.set("githubRedirectUrl", RedirectUrl);
Cookies.set("api_url", this.baseURL);
return {success: true, redirectUrl: RedirectUrl, providerUrl: ProviderUrl}
default:
const login = await axios.post(this.baseURL+"/signin", {
email: email,
password: password,
})
const {jwt, refresh} = login.data;
Cookies.set("jwt", jwt, { expires: 7 });
Cookies.set("refresh", refresh, { expires: 7 });
return {success: true};
}
}
logout () {
Cookies.remove("jwt");
Cookies.remove("refresh");
}
async register ({email, password}) {
const signup = await axios.post(this.baseURL+"/signup", {
email: email,
password: password,
})
const {jwt, refresh} = signup.data;
Cookies.set("jwt", jwt, { expires: 7 });
Cookies.set("refresh", refresh, { expires: 7 });
return true;
}
async refresh() {
const refreshResp = await axios.post(this.baseURL+"/refresh-token", {
access: Cookies.get("jwt"),
refresh: Cookies.get("refresh"),
})
const {jwt, refresh} = refreshResp;
Cookies.set("jwt", jwt, { expires: 7 });
Cookies.set("refresh", refresh, { expires: 7 });
return true;
}
async setUser() {
const cookies = await axios.get(this.baseURL + "/tokens");
const { jwt, refreshToken } = cookies.data;
Cookies.set("jwt", jwt, { expires: 7 });
Cookies.set("refresh", refreshToken, { expires: 7 });
}
getJWTToken () {
return Cookies.get("jwt");
}
getUserId () {
return Cookies.get("user_id");
}
isAuthenticated () {
if (Cookies.get("jwt")) {
return true;
}
return false;
}
}
22 changes: 22 additions & 0 deletions RocketsClient.js
@@ -0,0 +1,22 @@
import RocketsAuth from "./RocketsAuth"

export default class RocketsClient{

constructor(config) {
if (!config.baseURL) {
throw "Please specify a baseURL. More information at https://rocketsgraphql.com/docs/libraries/rockets-js-sdk#setup.";
}

this.baseURL = config.baseURL;

this.auth = new RocketsAuth({
baseURL: config.baseURL,
})

if (this.auth.isAuthenticated()) {
// if this is authenticated, keep refreshing tokens
this.auth.refresh()
setTimeout(() => this.auth.refresh(), 5 * 60)
}
}
}
40 changes: 4 additions & 36 deletions index.js
@@ -1,39 +1,7 @@
import axios from "axios";
import Cookies from 'js-cookie'
import RocketsClient from "./RocketsClient"

const js = {
baseURL: "",
configure: (obj) => {
if (obj && obj.hasOwnProperty("baseURL")) {
if (obj["baseURL"].constructor.name === "String") {
js.baseURL = obj["baseURL"]
}
}
},
login: ({email, password}) => {
return axios.post(js.baseURL+"/signin", {
email: email,
password: password,
})
},
logout: () => {
Cookies.remove("jwt");
},
register: ({email, password}) => {
return axios.post(js.baseURL+"/signup", {
email: email,
password: password,
})
},
getJWTToken: () => {
return Cookies.get("jwt");
},
isAuthenticated: () => {
if (Cookies.get("jwt")) {
return true;
}
return false;
},
const createClient = (config) => {
return new RocketsClient(config)
}

export default js;
export { createClient, RocketsClient };
6 changes: 3 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "@kaushik_varanasi/rocketsgraphql-js-sdk",
"version": "1.0.5",
"description": "JS SDK to use RocketsGraphQl APIs",
"name": "@rocketgraphql/rocketgraph-js-sdk",
"version": "0.3.0",
"description": "JS SDK to use RocketGraphQl APIs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down

0 comments on commit ac41b2b

Please sign in to comment.