Skip to content

Commit cca93fa

Browse files
committed
✨ feat : Add recoil-persist for state persistence
- Add the Recoil-persist library - Add functionality to userState to maintain the logged-in state Related issue: YJU-OKURA#115
1 parent 38c34bd commit cca93fa

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"react-markdown": "^9.0.1",
3535
"react-pdf": "^7.7.0",
3636
"recoil": "^0.7.7",
37+
"recoil-persist": "^5.1.0",
3738
"swiper": "^11.1.0",
3839
"ts-node": "^10.9.2"
3940
},

src/app/intro/googleLogin/page.tsx

+2-6
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,18 @@ import {useEffect} from 'react';
33
import Image from 'next/image';
44
import {useRouter, useSearchParams} from 'next/navigation';
55
import {useSetRecoilState} from 'recoil';
6-
import {userState} from '@/src/recoil/atoms/userState';
6+
import userState from '@/src/recoil/atoms/userState';
77
import accessTokenState from '@/src/recoil/atoms/accessTokenState';
88
import refreshTokenState from '@/src/recoil/atoms/refreshTokenState';
9-
import isLoginState from '@/src/recoil/atoms/isLoginState';
10-
import useRecoilStateWithLocalStorage from '@/src/hooks/useRecoilStateWithLocalStorage';
119
import postGoogleLogin from '@/src/api/auth/postGoogleLogin';
1210
import gifs from '@/public/gif';
1311
import '@/src/styles/variable.css';
1412

1513
const Page = () => {
1614
const code: string | null = useSearchParams().get('code');
17-
const [, setUser] = useRecoilStateWithLocalStorage(userState);
15+
const setUser = useSetRecoilState(userState);
1816
const setAccessToken = useSetRecoilState(accessTokenState);
1917
const setRefreshToken = useSetRecoilState(refreshTokenState);
20-
const setIsLogin = useSetRecoilState(isLoginState);
2118
const router = useRouter();
2219
useEffect(() => {
2320
console.log(code);
@@ -31,7 +28,6 @@ const Page = () => {
3128
setRefreshToken({refresh_token: res.refresh_token});
3229
window.localStorage.setItem('access_token', res.access_token);
3330
window.localStorage.setItem('refresh_token', res.refresh_token);
34-
setIsLogin(true);
3531
}
3632
router.push('/');
3733
});

src/recoil/atoms/userState.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import {atom} from 'recoil';
22
import {User} from '@/src/interfaces/user';
3+
import {recoilPersist} from 'recoil-persist';
4+
5+
const {persistAtom} = recoilPersist();
36

47
const userState = atom<User | null>({
58
key: 'userState',
69
default: null,
10+
effects_UNSTABLE: [persistAtom],
711
});
812

9-
const saveStateToLocalStorage = (key: string, value: unknown) => {
10-
localStorage.setItem(key, JSON.stringify(value));
11-
};
12-
13-
const loadStateFromLocalStorage = (key: string) => {
14-
const storedState = localStorage.getItem(key);
15-
return storedState ? JSON.parse(storedState) : null;
16-
};
17-
18-
export {userState, saveStateToLocalStorage, loadStateFromLocalStorage};
13+
export default userState;

yarn.lock

+33-3
Original file line numberDiff line numberDiff line change
@@ -5961,6 +5961,11 @@ readdirp@~3.6.0:
59615961
dependencies:
59625962
picomatch "^2.2.1"
59635963

5964+
recoil-persist@^5.1.0:
5965+
version "5.1.0"
5966+
resolved "https://registry.yarnpkg.com/recoil-persist/-/recoil-persist-5.1.0.tgz#c4232fe04f2e4b6afcc815baff56f2521f6dcde1"
5967+
integrity sha512-sew4k3uBVJjRWKCSFuBw07Y1p1pBOb0UxLJPxn4G2bX/9xNj+r2xlqYy/BRfyofR/ANfqBU04MIvulppU4ZC0w==
5968+
59645969
recoil@^0.7.7:
59655970
version "0.7.7"
59665971
resolved "https://registry.yarnpkg.com/recoil/-/recoil-0.7.7.tgz#c5f2c843224384c9c09e4a62c060fb4c1454dc8e"
@@ -6407,7 +6412,16 @@ string-length@^4.0.1:
64076412
char-regex "^1.0.2"
64086413
strip-ansi "^6.0.0"
64096414

6410-
"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
6415+
"string-width-cjs@npm:string-width@^4.2.0":
6416+
version "4.2.3"
6417+
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
6418+
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
6419+
dependencies:
6420+
emoji-regex "^8.0.0"
6421+
is-fullwidth-code-point "^3.0.0"
6422+
strip-ansi "^6.0.1"
6423+
6424+
"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
64116425
version "4.2.3"
64126426
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
64136427
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
@@ -6491,7 +6505,14 @@ stringify-entities@^4.0.0:
64916505
character-entities-html4 "^2.0.0"
64926506
character-entities-legacy "^3.0.0"
64936507

6494-
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", strip-ansi@^6.0.0, strip-ansi@^6.0.1:
6508+
"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
6509+
version "6.0.1"
6510+
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
6511+
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
6512+
dependencies:
6513+
ansi-regex "^5.0.1"
6514+
6515+
strip-ansi@^6.0.0, strip-ansi@^6.0.1:
64956516
version "6.0.1"
64966517
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
64976518
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
@@ -7197,7 +7218,16 @@ wide-align@^1.1.2:
71977218
dependencies:
71987219
string-width "^1.0.2 || 2 || 3 || 4"
71997220

7200-
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
7221+
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
7222+
version "7.0.0"
7223+
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
7224+
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
7225+
dependencies:
7226+
ansi-styles "^4.0.0"
7227+
string-width "^4.1.0"
7228+
strip-ansi "^6.0.0"
7229+
7230+
wrap-ansi@^7.0.0:
72017231
version "7.0.0"
72027232
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
72037233
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==

0 commit comments

Comments
 (0)