Skip to content

Commit 1eeda1a

Browse files
committed
social
1 parent 407658f commit 1eeda1a

37 files changed

Lines changed: 16294 additions & 19485 deletions

auth/package-lock.json

Lines changed: 0 additions & 16621 deletions
This file was deleted.

auth/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"private": true,
55
"dependencies": {
66
"@apollo/client": "^3.4.13",
7-
"@kaushik_varanasi/react-apollo": "^2.0.0",
8-
"@kaushik_varanasi/rocketsgraphql-js-sdk": "^1.3.2",
7+
"@rocketgraphql/react-apollo": "^2.1.0",
8+
"@rocketgraphql/rocketgraph-js-sdk": "^0.4.0",
99
"@testing-library/jest-dom": "^5.11.4",
1010
"@testing-library/react": "^11.1.0",
1111
"@testing-library/user-event": "^12.1.10",

auth/src/components/login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export function Login(props) {
1212

1313
// login
1414
try {
15-
await auth.login({email, password});
15+
await auth.signIn({email, password, provider: "local"});
1616
} catch (error) {
1717
alert("error logging in");
1818
console.error(error);

auth/src/components/signup.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export function Signup(props) {
1919
return;
2020
}
2121

22-
// redirect back to `/`
23-
history.push("/");
2422
}
2523

2624
return (

auth/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { Login } from "./components/login";
55
import { Signup } from "./components/signup";
66
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
77
import App from "./App";
8-
import { RApolloProvider } from "@kaushik_varanasi/react-apollo";
8+
import { RApolloProvider } from "@rocketgraphql/react-apollo";
99
import { auth } from "./utils/rockets";
1010

1111
ReactDOM.render(
1212
<React.StrictMode>
13-
<RApolloProvider auth={auth} gqlEndpoint="https://hasura-6khqdzs.rocketgraph.app/v1/graphql">
13+
<RApolloProvider auth={auth} gqlEndpoint="https://hasura-1cbijog.rocketgraph.app/v1/graphql">
1414
<Router>
1515
<Switch>
1616
<Route exact path="/login">

auth/src/utils/extra/RocketsAuth.js

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,92 @@
22
import axios from "axios";
33
import Cookies from 'js-cookie';
44

5-
axios.defaults.withCredentials = true;
5+
axios.defaults.withCredentials = false;
6+
7+
(async () => {
8+
if (window.location.href == Cookies.get('githubRedirectUrl')) {
9+
const cookies = await axios.get(Cookies.get("api_url")+ "/tokens");
10+
const { access, refresh } = cookies.data;
11+
Cookies.set("jwt", access, { expires: 7 });
12+
Cookies.set("refresh", refresh, { expires: 7 });
13+
}
14+
})().catch(err => {
15+
console.error(err);
16+
});
17+
18+
619
export default class Auth {
720
constructor(config) {
821
const { baseURL } = config;
922
this.baseURL = baseURL;
1023
}
11-
async login ({email, password}) {
12-
const login = await axios.post(this.baseURL+"/signin", {
13-
email: email,
14-
password: password,
15-
})
16-
const {ID, Token} = login.data;
17-
Cookies.set("jwt", Token, { expires: 7 });
18-
Cookies.set("user_id", ID, { expires: 7 });
19-
return true;
24+
async login ({email, password, provider} = {}) {
25+
if (provider == "local") {
26+
const login = await axios.post(this.baseURL+"/signin", {
27+
email: email,
28+
password: password,
29+
})
30+
const {jwt, refresh} = login.data;
31+
Cookies.set("jwt", jwt, { expires: 7 });
32+
Cookies.set("refresh", refresh, { expires: 7 });
33+
return true;
34+
}
35+
}
36+
async signIn ({email, password, provider} = {}) {
37+
switch (provider) {
38+
case "github":
39+
// first get the redirect URL
40+
// and provider URL
41+
console.log("getting github client test", this.baseURL);
42+
const client = await axios.get(this.baseURL+"/github/client")
43+
console.log("client: ", client)
44+
const {ProviderUrl, RedirectUrl} = client.data;
45+
// create a listener to update
46+
// cookies when redirectURL is reached
47+
Cookies.set("githubRedirectUrl", RedirectUrl);
48+
Cookies.set("api_url", this.baseURL);
49+
return {success: true, redirectUrl: RedirectUrl, providerUrl: ProviderUrl}
50+
default:
51+
const login = await axios.post(this.baseURL+"/signin", {
52+
email: email,
53+
password: password,
54+
})
55+
const {jwt, refresh} = login.data;
56+
Cookies.set("jwt", jwt, { expires: 7 });
57+
Cookies.set("refresh", refresh, { expires: 7 });
58+
return {success: true};
59+
}
2060
}
2161
logout () {
2262
Cookies.remove("jwt");
63+
Cookies.remove("refresh");
2364
}
2465
async register ({email, password}) {
2566
const signup = await axios.post(this.baseURL+"/signup", {
2667
email: email,
2768
password: password,
2869
})
29-
const {ID, Token} = signup.data;
30-
Cookies.set("jwt", Token, { expires: 7 });
31-
Cookies.set("user_id", ID, { expires: 7 });
70+
const {jwt, refresh} = signup.data;
71+
Cookies.set("jwt", jwt, { expires: 7 });
72+
Cookies.set("refresh", refresh, { expires: 7 });
3273
return true;
3374
}
75+
async refresh() {
76+
const refreshResp = await axios.post(this.baseURL+"/refresh-token", {
77+
access: Cookies.get("jwt"),
78+
refresh: Cookies.get("refresh"),
79+
})
80+
const {jwt, refresh} = refreshResp;
81+
Cookies.set("jwt", jwt, { expires: 7 });
82+
Cookies.set("refresh", refresh, { expires: 7 });
83+
return true;
84+
}
85+
async setUser() {
86+
const cookies = await axios.get(this.baseURL + "/tokens");
87+
const { jwt, refreshToken } = cookies.data;
88+
Cookies.set("jwt", jwt, { expires: 7 });
89+
Cookies.set("refresh", refreshToken, { expires: 7 });
90+
}
3491
getJWTToken () {
3592
return Cookies.get("jwt");
3693
}

auth/src/utils/extra/RocketsClient.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ export default class RocketsClient{
88
}
99

1010
this.baseURL = config.baseURL;
11+
console.log(this.baseURL);
1112

1213
this.auth = new RocketsAuth({
1314
baseURL: config.baseURL,
1415
})
16+
17+
if (this.auth.isAuthenticated()) {
18+
// if this is authenticated, keep refreshing tokens
19+
this.auth.refresh()
20+
setTimeout(() => this.auth.refresh(), 5 * 60)
21+
}
1522
}
1623
}

auth/src/utils/extra/rocketsgraphql-js-sdk.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import RocketsClient from "./RocketsClient"
22

33
const createClient = (config) => {
4+
console.log(config)
45
return new RocketsClient(config)
56
}
67

auth/src/utils/rockets.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { createClient } from "@kaushik_varanasi/rocketsgraphql-js-sdk";
1+
import { createClient } from "@rocketgraphql/rocketgraph-js-sdk";
22
import Cookies from 'js-cookie';
33

44
const config = {
5-
baseURL: "https://backend-6KHQDZS.rocketgraph.app/api",
5+
baseURL: "https://backend-1CBIJOG.rocketgraph.app/api",
66
};
77

8+
console.log("loading from extra");
89
if (Cookies.get("jwt")) {
910
console.log("got it");
1011
} else {

0 commit comments

Comments
 (0)