otp: register the token after success login
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||
import { ConfirmOTPResponse } from "pages/otp/types";
|
||||
import { baseApi } from "redux/api/apiSlice";
|
||||
import { AppThunk } from "redux/store";
|
||||
import { ACCESS_TOKEN, USER_NAME } from "utils/constants";
|
||||
import { LoginResponseType, UserType } from "utils/types/appTypes";
|
||||
import { ACCESS_TOKEN } from "utils/constants";
|
||||
|
||||
export type AuthState = {
|
||||
user: UserType | undefined;
|
||||
user: any;
|
||||
isActivated: boolean;
|
||||
loading: boolean;
|
||||
loginFailed: boolean;
|
||||
@@ -43,19 +43,11 @@ export const authSlice = createSlice({
|
||||
state.loginFailed = false;
|
||||
state.loaded = false;
|
||||
},
|
||||
loginSuccess(state, action: PayloadAction<LoginResponseType>) {
|
||||
if (action.payload.token) {
|
||||
localStorage.setItem(ACCESS_TOKEN, action.payload.token);
|
||||
state.token = action.payload.token;
|
||||
if (state.user?.username)
|
||||
localStorage.setItem(USER_NAME, state.user.username);
|
||||
|
||||
state.isActivated = true;
|
||||
state.loading = false;
|
||||
state.loginFailed = false;
|
||||
state.expiredToken = false;
|
||||
state.error = "";
|
||||
state.loaded = true;
|
||||
loginSuccess(state, action: PayloadAction<ConfirmOTPResponse>) {
|
||||
if (action.payload.result.access_token) {
|
||||
localStorage.setItem(ACCESS_TOKEN, action.payload.result.access_token);
|
||||
state.token = action.payload.result.access_token;
|
||||
localStorage.setItem("customer", JSON.stringify(action.payload.result.customer));
|
||||
}
|
||||
},
|
||||
logout: (state) => {
|
||||
|
||||
@@ -3,15 +3,17 @@ import OtpIcon from "components/Icons/otpIcon.tsx";
|
||||
import OtpInput from "components/OtpInput/OtpInput";
|
||||
import ProText from "components/ProText";
|
||||
import ProTitle from "components/ProTitle";
|
||||
import { loginSuccess } from "features/auth/authSlice";
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
import { useConfirmOtpMutation, useSendOtpMutation } from "redux/api/auth";
|
||||
import { useAppDispatch } from "redux/hooks";
|
||||
import { ProGray1 } from "ThemeConstants";
|
||||
import { ACCESS_TOKEN } from "utils/constants";
|
||||
|
||||
export default function OtpPage() {
|
||||
const { subdomain } = useParams();
|
||||
const dispatch = useAppDispatch();
|
||||
const { t } = useTranslation();
|
||||
const [sendOtp, { isLoading }] = useSendOtpMutation();
|
||||
const [confirmOtp, { isLoading: isConfirmLoading }] = useConfirmOtpMutation();
|
||||
@@ -81,11 +83,7 @@ export default function OtpPage() {
|
||||
confirmOtp({ otp: otp, phone: localStorage.getItem("userPhone") })
|
||||
.unwrap()
|
||||
.then((response) => {
|
||||
localStorage.setItem(
|
||||
"customer",
|
||||
JSON.stringify(response.result.customer),
|
||||
);
|
||||
localStorage.setItem(ACCESS_TOKEN, response.result.access_token);
|
||||
dispatch(loginSuccess(response));
|
||||
message.info(t("otp.confirmOTPSuccess"));
|
||||
navigate(`/${subdomain}/menu`);
|
||||
});
|
||||
|
||||
@@ -132,13 +132,7 @@ export type LinkParameterType = {
|
||||
sign: "=" | ">" | ">=" | "<" | "<=";
|
||||
value: string;
|
||||
};
|
||||
|
||||
export type LoginResponseType = {
|
||||
status: string;
|
||||
token?: string;
|
||||
error?: string;
|
||||
};
|
||||
|
||||
|
||||
export type LoginUserType = {
|
||||
username?: string;
|
||||
password?: string;
|
||||
|
||||
Reference in New Issue
Block a user