mirror of
https://github.com/lemeow125/StudE-Frontend.git
synced 2025-06-28 16:25:46 +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
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import * as React from "react";
|
||||
import { Text, Pressable, GestureResponderEvent } from "react-native";
|
||||
import { Pressable, GestureResponderEvent } from "react-native";
|
||||
import styles from "../../styles";
|
||||
|
||||
export interface props {
|
||||
|
|
|
@ -1,18 +1,24 @@
|
|||
export default function ParseError(text: string) {
|
||||
return text
|
||||
.replaceAll(/[{}()"]/g, " ")
|
||||
.replaceAll(/,/g, "\n")
|
||||
.replaceAll("[", "")
|
||||
.replaceAll("]", "")
|
||||
.replaceAll(".", "");
|
||||
if (text) {
|
||||
return text
|
||||
.replaceAll(/[{}()"]/g, " ")
|
||||
.replaceAll(/,/g, "\n")
|
||||
.replaceAll("[", "")
|
||||
.replaceAll("]", "")
|
||||
.replaceAll(".", "");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
export function ParseLoginError(text: string) {
|
||||
return text
|
||||
.replaceAll(/[{}()"]/g, " ")
|
||||
.replaceAll(/,/g, "\n")
|
||||
.replaceAll("[", "")
|
||||
.replaceAll("]", "")
|
||||
.replaceAll(".", "")
|
||||
.replaceAll("non_field_errors", "");
|
||||
if (text) {
|
||||
return text
|
||||
.replaceAll(/[{}()"]/g, " ")
|
||||
.replaceAll(/,/g, "\n")
|
||||
.replaceAll("[", "")
|
||||
.replaceAll("]", "")
|
||||
.replaceAll(".", "")
|
||||
.replaceAll("non_field_errors", "");
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
|
0
src/components/Validation/Validation.tsx
Normal file
0
src/components/Validation/Validation.tsx
Normal file
Loading…
Add table
Add a link
Reference in a new issue