-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters