Polished login

This commit is contained in:
Keannu Bernasol 2023-07-03 22:40:58 +08:00
parent a634f07f2b
commit 4faf222b52
5 changed files with 123 additions and 18 deletions

View file

@ -40,7 +40,7 @@ export function UserLogin(user: LoginParams) {
.post("/api/v1/accounts/token/login/", user) .post("/api/v1/accounts/token/login/", user)
.then(async (response) => { .then(async (response) => {
AsyncStorage.setItem("token", JSON.stringify(response.data.auth_token)); AsyncStorage.setItem("token", JSON.stringify(response.data.auth_token));
return [true]; return [true, JSON.stringify(response.data.auth_token)];
}) })
.catch((error) => { .catch((error) => {
console.log( console.log(

View file

@ -13,7 +13,7 @@ export const AuthSlice = createSlice({
}, },
reducers: { reducers: {
setToken: (state, action) => { setToken: (state, action) => {
state.creds.token = action.payload.token; state.creds.token = action.payload;
}, },
setUser: (state, action) => { setUser: (state, action) => {
state.creds = { state.creds = {

View file

@ -1,20 +1,16 @@
import * as React from "react"; import * as React from "react";
import styles from "../../styles"; import styles from "../../styles";
import { font_sizes } from "../../styles";
import { View, Text } from "react-native"; import { View, Text } from "react-native";
import { useSelector } from "react-redux";
import { RootState } from "../../features/redux/Store/Store";
export default function Home() { export default function Home() {
const creds = useSelector((state: RootState) => state.auth.creds);
return ( return (
<View style={styles.background}> <View style={styles.background}>
<Text <Text style={styles.text_white_large}>Template Homepage</Text>
style={{ <Text style={styles.text_white_tiny}>{JSON.stringify(creds)}</Text>
fontSize: font_sizes.large, <Text style={styles.text_white_tiny}>Token: {creds.token}</Text>
color: "white",
textAlign: "center",
}}
>
Template Homepage
</Text>
</View> </View>
); );
} }

View file

@ -9,10 +9,7 @@ import {
} from "react-native"; } from "react-native";
import { useSelector, useDispatch } from "react-redux"; import { useSelector, useDispatch } from "react-redux";
import { RootState } from "../../features/redux/Store/Store"; import { RootState } from "../../features/redux/Store/Store";
import { import { setToken } from "../../features/redux/slices/AuthSlice/AuthSlice";
setUser,
clear,
} from "../../features/redux/slices/AuthSlice/AuthSlice";
import { colors } from "../../styles"; import { colors } from "../../styles";
import { useState } from "react"; import { useState } from "react";
import LoginIcon from "../../icons/LoginIcon/LoginIcon"; import LoginIcon from "../../icons/LoginIcon/LoginIcon";
@ -24,8 +21,8 @@ import { ParseLoginError } from "../../components/ParseError/ParseError";
export default function Login() { export default function Login() {
const navigation = useNavigation<RootDrawerParamList>(); const navigation = useNavigation<RootDrawerParamList>();
// const dispatch = useDispatch(); const dispatch = useDispatch();
// const creds = useSelector((state: RootState) => state.auth.creds); const creds = useSelector((state: RootState) => state.auth.creds);
const [user, setUser] = useState({ const [user, setUser] = useState({
username: "", username: "",
password: "", password: "",
@ -85,6 +82,8 @@ export default function Login() {
}).then((result) => { }).then((result) => {
if (result[0]) { if (result[0]) {
setUser({ ...user, username: "", password: "", error: "" }); setUser({ ...user, username: "", password: "", error: "" });
console.log("Token:", result[1]);
dispatch(setToken(result[1]));
navigation.navigate("Home"); navigation.navigate("Home");
} else { } else {
setUser({ setUser({

View file

@ -0,0 +1,110 @@
import * as React from "react";
import styles from "../../styles";
import {
View,
Text,
TextInput,
NativeSyntheticEvent,
TextInputChangeEventData,
} from "react-native";
import { useSelector, useDispatch } from "react-redux";
import { RootState } from "../../features/redux/Store/Store";
import {
setUser,
clear,
} from "../../features/redux/slices/AuthSlice/AuthSlice";
import { colors } from "../../styles";
import { useState } from "react";
import LoginIcon from "../../icons/LoginIcon/LoginIcon";
import Button from "../../components/Button/Button";
import { useNavigation } from "@react-navigation/native";
import { RootDrawerParamList } from "../../interfaces/Interfaces";
import { UserLogin } from "../../components/Api/Api";
import { ParseLoginError } from "../../components/ParseError/ParseError";
export default function Onboarding() {
const navigation = useNavigation<RootDrawerParamList>();
// const dispatch = useDispatch();
// const creds = useSelector((state: RootState) => state.auth.creds);
const [user, setUser] = useState({
username: "",
password: "",
error: "",
});
return (
<View style={styles.background}>
<View style={styles.container}>
<View
style={{
paddingVertical: 4,
marginBottom: 16,
borderRadius: 4,
width: "90%",
backgroundColor: colors.blue_1,
}}
/>
<View style={styles.flex_row}>
<LoginIcon size={32} />
<Text style={styles.text_white_large}>Student Login</Text>
</View>
<View style={{ paddingVertical: 8 }} />
<TextInput
style={styles.text_input}
placeholder="Username"
placeholderTextColor="white"
autoCapitalize="none"
value={user.username}
onChange={(
e: NativeSyntheticEvent<TextInputChangeEventData>
): void => {
setUser({ ...user, username: e.nativeEvent.text });
}}
/>
<View style={{ paddingVertical: 4 }} />
<TextInput
style={styles.text_input}
placeholder="Password"
placeholderTextColor="white"
secureTextEntry={true}
value={user.password}
onChange={(
e: NativeSyntheticEvent<TextInputChangeEventData>
): void => {
setUser({ ...user, password: e.nativeEvent.text });
}}
/>
<View style={{ paddingVertical: 2 }} />
<Text style={styles.text_white_small}>{user.error}</Text>
<View style={{ paddingVertical: 4 }} />
<Button
onPress={async () => {
await UserLogin({
username: user.username,
password: user.password,
}).then((result) => {
if (result[0]) {
setUser({ ...user, username: "", password: "", error: "" });
navigation.navigate("Home");
} else {
setUser({
...user,
error: ParseLoginError(JSON.stringify(result[1])),
});
}
});
}}
color={colors.blue_3}
>
<Text style={styles.text_white_small}>Login</Text>
</Button>
<Button
onPress={() => navigation.navigate("Register")}
color={colors.blue_3}
>
<Text style={styles.text_white_small}>Register</Text>
</Button>
</View>
</View>
);
}