-
Notifications
You must be signed in to change notification settings - Fork 0
/
api-server.js
101 lines (88 loc) · 3.7 KB
/
api-server.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
const express = require('express')
const app = express()
const port = 3002
let supertokens = require("supertokens-node");
let Session = require("supertokens-node/recipe/session");
let ThirdPartyEmailPassword = require("supertokens-node/recipe/thirdpartyemailpassword");
let {Google, Github, Facebook} = ThirdPartyEmailPassword;
let cors = require("cors");
require('dotenv').config()
supertokens.init({
supertokens: {
connectionURI: process.env.SUPERTOKENS_CONNECTION_URI,
apiKey: process.env.SUPERTOKENS_API_KEY
},
appInfo: {
// learn more about this on https://supertokens.io/docs/thirdpartyemailpassword/appinfo
appName: "NewsDux",
apiDomain: "localhost:3002",
websiteDomain: "localhost:3000",
websiteBasePath: "/login"
},
recipeList: [
ThirdPartyEmailPassword.init({
override: {
apis: (originalImplementation) => {
return {
...originalImplementation,
signInUpPOST: async (input) => {
// First we call the original implementation of signInUpPOST.
let response = await originalImplementation.signInUpPOST(input);
// Post sign up response, we check if it was successful
if (response.status === "OK") {
let { id, email } = response.user;
// Then we check if the user signed up using email / password or a third party login provider.
if (response.type === "thirdparty") {
// This is the response from the OAuth 2 provider that contains their tokens or user info.
let thirdPartyAuthCodeResponse = response.authCodeResponse;
console.log(thirdPartyAuthCodeResponse)
}
if (input.type === "emailpassword") {
// These are the input form fields values that the user used while signing up / in
let formFields = input.formFields;
console.log(formFields)
}
if (response.createdNewUser) {
// TODO: Post sign up logic
} else {
// TODO: Post sign in logic
}
}
return response;
}
}
}
},
signUpFeature: {
formFields: [{
id: "name"
}]
},
providers: [
Google({
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
clientId: process.env.GOOGLE_CLIENT_ID
}),
Github({
clientSecret: process.env.GITHUB_CLIENT_SECRET,
clientId: process.env.GITHUB_CLIENT_ID
})
]
}),
Session.init() // initializes session features
]
});
app.use(cors({
origin: 'http://localhost:3000',
allowedHeaders: ["content-type", ...supertokens.getAllCORSHeaders() ],
methods: ["GET", "PUT", "POST", "DELETE"],
credentials: true,
}));
app.use(supertokens.middleware());
app.use(supertokens.errorHandler())
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})