-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { auth, syncroom } from "@/common/axios"; | ||
|
||
export const login = async (username: string, password: string) => { | ||
const { | ||
data: { key }, | ||
} = await auth.post<{ | ||
key: string; | ||
}>("/login", { username, password }); | ||
|
||
const credentials = await syncroom.get<{ | ||
token: string; | ||
refreshToken: string; | ||
}>("/token", { | ||
params: { key }, | ||
}); | ||
|
||
return credentials.data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useMutation } from "react-query"; | ||
import { login } from "@/api/endpoints/auth"; | ||
|
||
export const useAuth = () => | ||
useMutation< | ||
{ | ||
token: string; | ||
refreshToken: string; | ||
}, | ||
unknown, | ||
{ | ||
username: string; | ||
password: string; | ||
} | ||
>(({ username, password }) => login(username, password)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import axios from "axios"; | ||
|
||
export const auth = axios.create({ | ||
baseURL: "https://auth.syncroom.link/api", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./auth"; | ||
export * from "./syncroom"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import axios from "axios"; | ||
import { useAuthStore } from "@/store/auth"; | ||
|
||
const syncroom = axios.create({ | ||
baseURL: "https://webapi.syncroom.appservice.yamaha.com/comm", | ||
}); | ||
|
||
syncroom.interceptors.request.use(config => { | ||
const { token } = useAuthStore.getState(); | ||
config.headers["Authorization"] = token; | ||
return config; | ||
}); | ||
|
||
export { syncroom }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import React, { useEffect } from "react"; | ||
import { useForm } from "react-hook-form"; | ||
import { z } from "zod"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { useAuthStore } from "@/store/auth"; | ||
import { useAuth } from "@/api/hooks/auth"; | ||
|
||
const signInSchema = z.object({ | ||
username: z.string().email(), | ||
password: z.string().min(1), | ||
}); | ||
|
||
export const SignInPage: React.FC = () => { | ||
const { | ||
register, | ||
handleSubmit, | ||
formState: { isValid, errors, isSubmitting }, | ||
} = useForm<{ | ||
username: string; | ||
password: string; | ||
}>({ mode: "onTouched", resolver: zodResolver(signInSchema) }); | ||
const setTokens = useAuthStore(state => state.setTokens); | ||
const { mutateAsync, data, isLoading } = useAuth(); | ||
|
||
const onSubmit = handleSubmit(data => mutateAsync(data)); | ||
|
||
useEffect(() => { | ||
if (data) setTokens(data); | ||
}, [data, setTokens]); | ||
|
||
return ( | ||
<main> | ||
<h1>Login</h1> | ||
|
||
<form onSubmit={onSubmit}> | ||
<input type="text" {...register("username")} /> | ||
{errors.username && <span>이메일 주소를 입력해 주세요</span>} | ||
<input type="password" {...register("password")} /> | ||
{errors.password && <span>비밀번호를 입력해 주세요</span>} | ||
<input type="submit" value="로그인" disabled={!isValid} /> | ||
{isLoading && <span>로딩중...</span>} | ||
{isSubmitting && <span>제출중...</span>} | ||
</form> | ||
</main> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./SignInPage"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { create } from "zustand"; | ||
import { createJSONStorage, persist } from "zustand/middleware"; | ||
|
||
interface AuthStore { | ||
token?: string; | ||
refreshToken?: string; | ||
setTokens: (credentials: { token: string; refreshToken: string }) => void; | ||
refreshTokens: () => void; | ||
} | ||
|
||
export const useAuthStore = create( | ||
persist<AuthStore>( | ||
set => ({ | ||
token: undefined, | ||
refreshToken: undefined, | ||
setTokens: tokens => set(tokens), | ||
refreshTokens: () => { | ||
/* TODO */ | ||
}, | ||
}), | ||
{ name: "auth", storage: createJSONStorage(() => localStorage) }, | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters