mirror of
https://github.com/lemeow125/StudE-Frontend.git
synced 2025-08-03 10:03:20 +08:00
Switch to JSON webtoken login format
This commit is contained in:
parent
4faf222b52
commit
11c4f0c264
9 changed files with 78 additions and 126 deletions
|
@ -37,19 +37,41 @@ export function UserRegister(register: RegistrationParams) {
|
|||
|
||||
export function UserLogin(user: LoginParams) {
|
||||
return instance
|
||||
.post("/api/v1/accounts/token/login/", user)
|
||||
.post("/api/v1/accounts/jwt/create/", user)
|
||||
.then(async (response) => {
|
||||
AsyncStorage.setItem("token", JSON.stringify(response.data.auth_token));
|
||||
return [true, JSON.stringify(response.data.auth_token)];
|
||||
console.log(response.data.access, response.data.refresh);
|
||||
AsyncStorage.setItem(
|
||||
"access_token",
|
||||
JSON.stringify(response.data.access)
|
||||
);
|
||||
AsyncStorage.setItem(
|
||||
"refresh_token",
|
||||
JSON.stringify(response.data.refresh)
|
||||
);
|
||||
return [
|
||||
true,
|
||||
JSON.stringify(response.data.access),
|
||||
JSON.stringify(response.data.refresh),
|
||||
];
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(
|
||||
"Login Failed: " + error.response.status + " " + error.response.data
|
||||
);
|
||||
console.log("Login Failed:" + error.response.data);
|
||||
return [false, error.response.data];
|
||||
});
|
||||
}
|
||||
|
||||
export function TokenRefresh(token: string) {
|
||||
return instance
|
||||
.post("/api/v1/accounts/jwt/refresh/", token)
|
||||
.then(async (response) => {
|
||||
AsyncStorage.setItem("token", JSON.stringify(response.data.auth_token));
|
||||
return true;
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("Login Failed: " + error);
|
||||
return false;
|
||||
});
|
||||
}
|
||||
export async function UserInfo() {
|
||||
const token = JSON.parse((await AsyncStorage.getItem("token")) || "{}");
|
||||
return instance
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue