generated from UoaWDCC/react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from UoaWDCC/don/WFJB2-108
Don/WFJB2-108: Route login/signup
- Loading branch information
Showing
4 changed files
with
72 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,41 @@ | ||
import {apiInstance} from "@/api/ApiInstance"; | ||
|
||
import { apiInstance } from '@/api/ApiInstance'; | ||
export async function login(email: string, password: string) { | ||
let userRole; | ||
try { | ||
const res = await apiInstance.get(`user/${email}/role`) | ||
userRole = res.data | ||
const res = await apiInstance.get(`user/${email}/role`); | ||
userRole = res.data; | ||
|
||
if (userRole == null) { | ||
throw Error("Invalid credentials"); | ||
if (!userRole) { | ||
throw Error('Invalid credentials'); | ||
} | ||
} catch (e) { | ||
throw Error("Invalid credentials") | ||
throw Error('Invalid credentials'); | ||
} | ||
|
||
let loginUrl; | ||
console.log(userRole) | ||
if (userRole === 'admin') { | ||
loginUrl = 'login-admin' | ||
loginUrl = 'login-admin'; | ||
} else if (userRole === 'alumni') { | ||
loginUrl = 'login-alumni' | ||
loginUrl = 'login-alumni'; | ||
} else if (userRole === 'member') { | ||
loginUrl = 'login-member' | ||
loginUrl = 'login-member'; | ||
} else if (userRole === 'sponsor') { | ||
loginUrl = 'login-sponsor' | ||
loginUrl = 'login-sponsor'; | ||
} else { | ||
throw Error("Unknown login type") | ||
throw Error('Unknown login type'); | ||
} | ||
|
||
try { | ||
const res = await apiInstance.post(loginUrl, { | ||
"email": email, | ||
"password": password | ||
}) | ||
const {userId, token} = res.data; | ||
localStorage.setItem('accessToken', token) | ||
console.log(`Successfully logged in as UserID ${userId}`) | ||
email, | ||
password, | ||
}); | ||
const { userId, token } = res.data; | ||
localStorage.setItem('accessToken', token); | ||
console.log(`Successfully logged in as UserID ${userId}`); | ||
|
||
// Todo: After successful login. Create endpoint to get whoami() and link that to redux state. | ||
return { userType: userRole }; // Return userType | ||
} catch (e) { | ||
throw Error("Invalid credentials") | ||
throw Error('Invalid credentials'); | ||
} | ||
} | ||
|
||
export async function logout() { | ||
localStorage.removeItem('accessToken') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|