-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
97 lines (80 loc) · 3.62 KB
/
auth.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
/* auth.js provides LOGIN-related functions */
"use strict";
const apiBaseURL = "http://microbloglite.us-east-2.elasticbeanstalk.com";
// Backup server (mirror): "https://microbloglite.onrender.com"
// NOTE: API documentation is available at /docs
// For example: http://microbloglite.us-east-2.elasticbeanstalk.com/docs
// You can use this function to get the login data of the logged-in
// user (if any). It returns either an object including the username
// and token, or an empty object if the visitor is not logged in.
function getLoginData() {
const loginJSON = window.localStorage.getItem("login-data");
return JSON.parse(loginJSON) || {};
}
// You can use this function to see whether the current visitor is
// logged in. It returns either `true` or `false`.
function isLoggedIn() {
const loginData = getLoginData();
return Boolean(loginData.token);
}
// This function is already being used in the starter code for the
// landing page, in order to process a user's login. READ this code,
// and feel free to re-use parts of it for other `fetch()` requests
// you may need to write.
function login(loginData) {
// POST /auth/login
const options = {
method: "POST",
headers: {
// This header specifies the type of content we're sending.
// This is required for endpoints expecting us to send
// JSON data.
"Content-Type": "application/json",
},
body: JSON.stringify(loginData),
};
return fetch(apiBaseURL + "/auth/login", options)
.then(response => response.json())
.then(loginData => {
if (loginData.message === "Invalid username or password") {
// console.error(loginData.message);
// console.log(loginData.message)
// https://stackoverflow.com/questions/9156176/what-is-the-difference-between-throw-new-error-and-throw-someobject
throw new Error(loginData.message)
// Here is where you might want to add an error notification
// or other visible indicator to the page so that the user is
// informed that they have entered the wrong login info.
}
window.localStorage.setItem("login-data", JSON.stringify(loginData));
window.location.assign("/posts"); // redirect
return loginData;
});
}
// This is the `logout()` function you will use for any logout button
// which you may include in various pages in your app. Again, READ this
// function and you will probably want to re-use parts of it for other
// `fetch()` requests you may need to write.
function logout() {
const loginData = getLoginData();
// GET /auth/logout
const options = {
method: "GET",
headers: {
// This header is how we authenticate our user with the
// server for any API requests which require the user
// to be logged-in in order to have access.
// In the API docs, these endpoints display a lock icon.
Authorization: `Bearer ${loginData.token}`,
},
};
fetch(apiBaseURL + "/auth/logout", options)
.then(response => response.json())
.then(data => console.log(data))
.finally(() => {
// We're using `finally()` so that we will continue with the
// browser side of logging out (below) even if there is an
// error with the fetch request above.
window.localStorage.removeItem("login-data"); // remove login data from LocalStorage
window.location.assign("/"); // redirect back to landing page
});
}