-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·173 lines (150 loc) · 4.27 KB
/
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const express = require("express");
const bodyParser = require("body-parser");
const fetch = require("node-fetch");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
require('dotenv').config()
const app = express();
const ZARYA_PORT = process.env.ZARYA_PORT || 3000;
app.use(bodyParser.json());
const HASURA_SIGNUP = `
mutation ($name: String!, $email: String!, $password: String!, $username: String!) {
insert_users_one(object: {name: $name, email: $email, password: $password, username: $username}) {
id
}
}
`;
const HASURA_LOGIN = `
query login($email: String!) {
users(where:{ email: {_eq: $email}}) {
email,
password,
id,
name
}
}
`
// execute the parent operation in Hasura
const execute = async (variables, query) => {
const hasuraSecret = process.env.HASURA_GRAPHQL_ADMIN_SECRET;
const meta = {
"Content-Type": "application/json",
"x-hasura-admin-secret": hasuraSecret,
};
const headers = meta
const baseUrl = process.env.HASURA_APP_URL
const fetchResponse = await fetch(
`${baseUrl}/v1/graphql`,
{
method: "POST",
headers: headers,
body: JSON.stringify({
query,
variables,
}),
}
);
const data = await fetchResponse.json();
return data;
};
// generate password in hash
const generatePassword = async (password) => {
const salt = await bcrypt.genSalt();
const hash = await bcrypt.hash(password, salt);
return hash;
};
const comparaPassword = async (password, hashPassword) => {
const isMatch = await bcrypt.compare(password, hashPassword)
return isMatch
}
app.post("/signup", async (req, res) => {
try {
if (!req.body.input) {
return res.status(400).json({
message: "is required input object"
});
}
// get request input
const { name, email, password, username } = req.body.input;
// run some business logic
const hash = await generatePassword(password);
// execute the Hasura operation
const { data, errors } = await execute({
name,
email,
password: hash,
username,
}, HASURA_SIGNUP);
// if Hasura operation errors, then throw error
if (errors) {
return res.status(400).json(errors[0]);
}
const tokenContents = {
sub: data.insert_users_one.id.toString(),
name: name,
iat: Date.now() / 1000,
iss: 'https://myapp.com/',
"https://hasura.io/jwt/claims": {
"x-hasura-allowed-roles": ["user"],
"x-hasura-user-id": data.insert_users_one.id.toString(),
"x-hasura-default-role": "user",
"x-hasura-role": "user"
},
exp: Math.floor(Date.now() / 1000) + (24 * 60 * 60)
}
const token = jwt.sign(tokenContents, process.env.ENCRYPTION_KEY);
// success
return res.json({
...data.insert_users_one,
token: token
});
} catch (err) {
return res.status(500).json(err);
}
});
app.post("/login", async (req, res) => {
try {
const { email, password } = req.body.input;
// execute the Hasura operation
const { data, errors } = await execute({
email,
}, HASURA_LOGIN);
// if Hasura operation errors, then throw error
if (errors) {
return res.status(400).json(errors[0]);
}
const isMatch = await comparaPassword(password, data.users[0].password)
if (!isMatch) {
return res.status(401).json({
message: `email or password is incorrect`
});
}
const tokenContents = {
sub: data.users[0].id.toString(),
name: data.users[0].name,
iat: Date.now() / 1000,
iss: 'https://myapp.com/',
"https://hasura.io/jwt/claims": {
"x-hasura-allowed-roles": ["user"],
"x-hasura-user-id": data.users[0].id.toString(),
"x-hasura-default-role": "user",
"x-hasura-role": "user"
},
exp: Math.floor(Date.now() / 1000) + (24 * 60 * 60)
}
const token = jwt.sign(tokenContents, process.env.ENCRYPTION_KEY);
return res.status(200).json({
id: data.users[0].id,
token: token
});
} catch (err) {
if (err instanceof TypeError) {
return res.status(400).json(err.message);
}
if (err.message) {
return res.status(500).json(err.message);
}
return res.status(500).json("Internal error");
}
})
app.listen(ZARYA_PORT);