-
Notifications
You must be signed in to change notification settings - Fork 0
/
RocketsAuth.js
121 lines (117 loc) · 4.3 KB
/
RocketsAuth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import axios from "axios";
import Cookies from 'js-cookie';
axios.defaults.withCredentials = false;
if (typeof window !== "undefined" && window && window.location.search) {
let params = new URLSearchParams(window.location.search);
if (params.get('rgraphRefreshToken')) {
const refreshToken = params.get('rgraphRefreshToken')
console.log("url.searchParams.get('rgraphRefreshToken')", refreshToken);
Cookies.set(("refresh", refreshToken, { expires: 365 }));
}
if (params.get('rgraphAccessToken')) {
const accessToken = params.get('rgraphAccessToken')
console.log("url.searchParams.get('rgraphAccessToken')", accessToken);
Cookies.set(("jwt", accessToken, { expires: 7 }));
}
}
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+"/login", {
email: email,
password: password,
})
const {access, refresh} = login.data;
Cookies.set("jwt", access, { 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+"/login", {
email: email,
password: password,
})
const {access, refresh} = login.data;
Cookies.set("jwt", access, { expires: 7 });
Cookies.set("refresh", refresh, { expires: 7 });
return {success: true};
}
}
async sendOTP({ phone }) {
const otp = await axios.post(this.baseURL + "/sendotp", {
Phone: phone,
})
}
async signInWithOTP({ phone, otp }) {
const login = await axios.post(this.baseURL + "/signin-with-otp", {
Phone: phone,
Otp: otp,
});
const { access, refresh } = login.data;
Cookies.set("jwt", access, { 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 {access, refresh} = signup.data;
Cookies.set("jwt", access, { 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 {access, refresh} = refreshResp.data;
console.log("access refresh", access, refresh);
Cookies.set("jwt", access, { expires: 7 });
Cookies.set("refresh", refresh, { expires: 7 });
return true;
}
async setUser() {
const cookies = await axios.get(this.baseURL + "/tokens");
const { access, refresh } = cookies.data;
Cookies.set("jwt", access, { expires: 7 });
Cookies.set("refresh", refresh, { expires: 7 });
}
getJWTToken () {
return Cookies.get("jwt");
}
getUserId () {
return Cookies.get("user_id");
}
isAuthenticated () {
if (Cookies.get("jwt")) {
return true;
}
return false;
}
}