Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
survey
Commits
aee1333e
Commit
aee1333e
authored
Jul 05, 2022
by
Yoon, Daeki
😅
Browse files
인증 컨텍스트 및 도우미 파일 추가
parent
e9794f84
Changes
4
Show whitespace changes
Inline
Side-by-side
frontend/src/apis/baseUrl.ts
0 → 100644
View file @
aee1333e
export
default
"
/api
"
;
frontend/src/apis/index.ts
0 → 100644
View file @
aee1333e
import
axios
from
"
axios
"
;
export
*
as
authApi
from
"
./auth.api
"
;
frontend/src/auth/auth.context.tsx
0 → 100644
View file @
aee1333e
import
React
,
{
createContext
,
FC
,
ReactNode
,
useContext
,
useState
,
}
from
"
react
"
;
import
{
IUser
}
from
"
../types
"
;
import
{
getLocalUser
,
handleLogin
,
handleLogout
}
from
"
./auth.helper
"
;
interface
IAuthContext
{
login
:
(
email
:
string
,
password
:
string
,
cb
?:
VoidFunction
)
=>
Promise
<
void
>
;
logout
:
(
cb
?:
VoidFunction
)
=>
Promise
<
void
>
;
user
:
IUser
;
}
const
AuthContext
=
createContext
<
IAuthContext
>
({
login
:
async
()
=>
{},
logout
:
async
()
=>
{},
user
:
{
isLoggedIn
:
false
},
});
export
const
AuthProvider
:
FC
<
{
children
:
ReactNode
}
>
=
({
children
})
=>
{
const
[
user
,
setUser
]
=
useState
(
getLocalUser
());
const
login
=
async
(
email
:
string
,
password
:
string
,
cb
:
VoidFunction
=
()
=>
{}
)
=>
{
const
user
=
await
handleLogin
(
email
,
password
);
setUser
(
user
);
cb
();
};
const
logout
=
async
(
cb
:
VoidFunction
=
()
=>
{})
=>
{
await
handleLogout
();
setUser
({
...
user
,
isLoggedIn
:
false
});
cb
();
};
return
(
<
AuthContext
.
Provider
value
=
{
{
login
,
logout
,
user
}
}
>
{
children
}
</
AuthContext
.
Provider
>
);
};
export
const
useAuth
=
()
=>
useContext
(
AuthContext
);
frontend/src/auth/auth.helper.ts
0 → 100644
View file @
aee1333e
import
{
authApi
}
from
"
../apis
"
;
import
{
IUser
}
from
"
../types
"
;
const
LOCAL_USER_INFO
=
"
survey-user-info
"
;
/**
* 1. 백엔드 로그인을 호출하여 로그인 정보를 얻습니다.
* 2. 로컬 저장소에 저장합니다.
* 3. 사용자 정보를 반환합니다.
* @param email 이메일
* @param password 비밀번호
* @returns 사용자 정보
*/
export
const
handleLogin
=
async
(
email
:
string
,
password
:
string
)
=>
{
const
user
:
IUser
=
await
authApi
.
login
(
email
,
password
);
// 로컬 저장소에는 로그인 여부만 저장
localStorage
.
setItem
(
LOCAL_USER_INFO
,
JSON
.
stringify
({
isLoggedIn
:
user
.
isLoggedIn
,
})
);
return
user
;
};
/**
* 로컬 저장소의 정보를 삭제합니다.
* 백엔드 로그아웃을 호출하여 쿠키를 제거합니다.
*/
export
const
handleLogout
=
async
()
=>
{
console
.
log
(
"
handle logout called
"
);
localStorage
.
removeItem
(
LOCAL_USER_INFO
);
try
{
await
authApi
.
logout
();
}
catch
(
error
)
{
console
.
log
(
"
logout 중에 에러 발생:
"
,
error
);
}
};
/**
* 1. 로컬 저장소에 저장된 사용자 로그인 정보를 반환합니다.
* 2. 로컬 저장소에 정보가 없으면 { isLoggedIn: false }를 반환합니다.
* @returns 로컬 저장소에 저장된 사용자 정보
*/
export
const
getLocalUser
=
()
=>
{
console
.
log
(
"
get local user called
"
);
const
userInfo
=
localStorage
.
getItem
(
LOCAL_USER_INFO
);
const
user
:
IUser
=
{
isLoggedIn
:
false
};
if
(
!
userInfo
)
{
return
user
;
}
const
userData
=
JSON
.
parse
(
userInfo
);
if
(
userData
.
isLoggedIn
)
{
user
.
isLoggedIn
=
true
;
}
else
{
user
.
isLoggedIn
=
false
;
}
return
user
;
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment