Skip to content

Commit

Permalink
feat: 로그인 유지 on/off 부활 (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
selfishAltruism committed Mar 1, 2024
1 parent a6afe14 commit 263e250
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/@types/user.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ declare namespace User {
export interface SignInRequestDto {
email: string;
password: string;
//auto?: boolean;
auto?: boolean;
}

export interface IsDuplicatedEmailResponseDto {
Expand Down
12 changes: 8 additions & 4 deletions src/configs/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,19 @@ export const resetAccess = (): unknown => delete API.defaults.headers['Authoriza

//Refresh
const storageRefreshKey = 'CAUCSE_JWT_REFRESH';
let isStored: boolean = true;

export const storeRefresh = (token: string): void => {
localStorage.setItem(storageRefreshKey, token);
export const storeRefresh = (auto: boolean, token: string): void => {
isStored = auto;
if (isStored) localStorage.setItem(storageRefreshKey, token);
else sessionStorage.setItem(storageRefreshKey, token);
};
export const removeRefresh = (): void => {
localStorage.removeItem(storageRefreshKey);
sessionStorage.removeItem(storageRefreshKey);
};
export const getRefresh = (): string | null => {
return localStorage.getItem(storageRefreshKey);
return localStorage.getItem(storageRefreshKey) ?? sessionStorage.getItem(storageRefreshKey);
};

API.interceptors.response.use(
Expand All @@ -51,7 +55,7 @@ API.interceptors.response.use(

setAccess(accessToken);
removeRefresh();
storeRefresh(refreshToken);
storeRefresh(isStored, refreshToken);

config.headers['Authorization'] = accessToken;
return API.request(config);
Expand Down
11 changes: 11 additions & 0 deletions src/pages/auth/signIn/SignInPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const SignInPage: React.FC = observer(() => {
defaultValues: {
email: '',
password: '',
auto: true,
},
});
const onSubmit = async (body: User.SignInRequestDto) => {
Expand Down Expand Up @@ -69,6 +70,16 @@ const SignInPage: React.FC = observer(() => {
)}
/>
<PasswordInput control={control} />
<Controller
name="auto"
control={control}
render={({ field }) => (
<CheckboxLabel
label="로그인 상태 유지"
control={<Checkbox {...field} size="small" defaultChecked={true} />}
/>
)}
/>
<LoginButton type="submit" $loading={isLoading} disabled={isDisabled}>
로그인
</LoginButton>
Expand Down
4 changes: 2 additions & 2 deletions src/stores/repositories/AuthRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AxiosResponse } from 'axios';

import { UserModel } from '../models/UserModel';

import { API, setAccess, storeRefresh, removeRefresh } from '@/configs/axios';
import { API, setAccess, storeRefresh } from '@/configs/axios';

class AuthRepo {
URI = '/api/v1/users';
Expand All @@ -16,7 +16,7 @@ class AuthRepo {
}>;

setAccess(accessToken);
storeRefresh(refreshToken);
storeRefresh(body.auto ?? false, refreshToken);
};

isDuplicatedEmail = async (email: string): Promise<boolean> => {
Expand Down

0 comments on commit 263e250

Please sign in to comment.