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