-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth-utils.js
37 lines (34 loc) · 974 Bytes
/
auth-utils.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
const fetch = require("cross-fetch");
// const cookie = require("cookie");
const chalk = require("chalk");
async function signUpUser(userName, password) {
const resp = await fetch(`${process.env.API_URL}/api/v1/users`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ userName, password }),
credentials: "include",
});
const data = await resp.json();
if (!resp.ok) {
throw new Error(data.message);
}
}
async function signInUser(userName, password) {
const resp = await fetch(`${process.env.API_URL}/api/v1/users/sessions`, {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ userName, password }),
credentials: "include",
});
if (!resp.ok) {
throw new Error(data.message);
}
return resp.json();
}
module.exports = { signInUser, signUpUser };