|
1 | 1 | // Interacts with the user_session_controller endpoints
|
2 |
| -import { Id } from "@/types/general"; |
| 2 | +import { |
| 3 | + defaultUserSession, |
| 4 | + isUserSession, |
| 5 | + parseUserSession, |
| 6 | + UserSession, |
| 7 | +} from "@/types/user-session"; |
3 | 8 | import { AxiosError, AxiosResponse } from "axios";
|
4 | 9 |
|
5 |
| -export const loginUser = async (email: string, password: string): Promise<[Id, string]> => { |
6 |
| - const axios = require("axios"); |
| 10 | +export const loginUser = async ( |
| 11 | + email: string, |
| 12 | + password: string |
| 13 | +): Promise<UserSession> => { |
| 14 | + const axios = require("axios"); |
7 | 15 |
|
8 |
| - console.log("email: ", email); |
9 |
| - console.log("password: ", password.replace(/./g, "*")); |
| 16 | + console.log("email: ", email); |
| 17 | + console.log("password: ", password.replace(/./g, "*")); |
10 | 18 |
|
11 |
| - var userId: Id = ""; |
12 |
| - var err: string = ""; |
| 19 | + var userSession: UserSession = defaultUserSession(); |
| 20 | + var err: string = ""; |
13 | 21 |
|
14 |
| - const data = await axios |
15 |
| - .post( |
16 |
| - "http://localhost:4000/login", |
17 |
| - { |
18 |
| - email: email, |
19 |
| - password: password, |
| 22 | + const data = await axios |
| 23 | + .post( |
| 24 | + "http://localhost:4000/login", |
| 25 | + { |
| 26 | + email: email, |
| 27 | + password: password, |
| 28 | + }, |
| 29 | + { |
| 30 | + withCredentials: true, |
| 31 | + headers: { |
| 32 | + "Content-Type": "application/x-www-form-urlencoded", |
20 | 33 | },
|
21 |
| - { |
22 |
| - withCredentials: true, |
23 |
| - headers: { |
24 |
| - "Content-Type": "application/x-www-form-urlencoded", |
25 |
| - }, |
26 |
| - setTimeout: 5000, // 5 seconds before timing out trying to log in with the backend |
27 |
| - } |
28 |
| - ) |
29 |
| - .then(function (response: AxiosResponse) { |
30 |
| - // handle success |
31 |
| - userId = response.data.data.id; |
32 |
| - }) |
33 |
| - .catch(function (error: AxiosError) { |
34 |
| - // handle error |
35 |
| - console.error(error.response?.status); |
36 |
| - if (error.response?.status == 401) { |
37 |
| - err = "Login failed: unauthorized"; |
38 |
| - } else { |
39 |
| - console.error(error); |
40 |
| - err = `Login failed: ${error.message}`; |
41 |
| - } |
42 |
| - }) |
| 34 | + setTimeout: 5000, // 5 seconds before timing out trying to log in with the backend |
| 35 | + } |
| 36 | + ) |
| 37 | + .then(function (response: AxiosResponse) { |
| 38 | + // handle success |
| 39 | + const userSessionData = response.data.data; |
| 40 | + if (isUserSession(userSessionData)) { |
| 41 | + userSession = parseUserSession(userSessionData); |
| 42 | + } |
| 43 | + }) |
| 44 | + .catch(function (error: AxiosError) { |
| 45 | + // handle error |
| 46 | + console.error(error.response?.status); |
| 47 | + if (error.response?.status == 401) { |
| 48 | + err = "Login failed: unauthorized"; |
| 49 | + } else { |
| 50 | + err = `Login failed: ${error.message}`; |
| 51 | + } |
| 52 | + }); |
43 | 53 |
|
44 |
| - return [userId, err]; |
45 |
| -} |
| 54 | + if (err) { |
| 55 | + console.error(err); |
| 56 | + throw err; |
| 57 | + } |
| 58 | + |
| 59 | + return userSession; |
| 60 | +}; |
46 | 61 |
|
47 | 62 | export const logoutUser = async (): Promise<string> => {
|
48 |
| - const axios = require("axios"); |
| 63 | + const axios = require("axios"); |
49 | 64 |
|
50 |
| - const data = await axios |
51 |
| - .get( |
52 |
| - "http://localhost:4000/logout", |
53 |
| - { |
54 |
| - withCredentials: true, |
55 |
| - setTimeout: 5000, // 5 seconds before timing out trying to log in with the backend |
56 |
| - } |
57 |
| - ) |
58 |
| - .then(function (response: AxiosResponse) { |
59 |
| - // handle success |
60 |
| - console.debug(response); |
61 |
| - }) |
62 |
| - .catch(function (error: AxiosError) { |
63 |
| - // handle error |
64 |
| - console.error(`Logout failed: ${error.message}`); |
65 |
| - return(`Logout failed: ${error.message}`); |
66 |
| - }) |
| 65 | + const data = await axios |
| 66 | + .get("http://localhost:4000/logout", { |
| 67 | + withCredentials: true, |
| 68 | + setTimeout: 5000, // 5 seconds before timing out trying to log in with the backend |
| 69 | + }) |
| 70 | + .then(function (response: AxiosResponse) { |
| 71 | + // handle success |
| 72 | + console.debug(response); |
| 73 | + }) |
| 74 | + .catch(function (error: AxiosError) { |
| 75 | + // handle error |
| 76 | + const err = `Logout failed: ${error.message}`; |
| 77 | + console.error(err); |
| 78 | + return err; |
| 79 | + }); |
67 | 80 |
|
68 |
| - return ""; |
69 |
| -} |
| 81 | + return ""; |
| 82 | +}; |
0 commit comments