|
|
|
|
@ -1,7 +1,8 @@
|
|
|
|
|
import { createSlice } from '@reduxjs/toolkit'
|
|
|
|
|
import type { PayloadAction } from '@reduxjs/toolkit'
|
|
|
|
|
import { createAppAsyncThunk } from './hooks'
|
|
|
|
|
import { networkApi, type ResponseData } from '../network'
|
|
|
|
|
import { type ResponseData } from '../network'
|
|
|
|
|
import { token } from '../utils'
|
|
|
|
|
|
|
|
|
|
interface AuthState {
|
|
|
|
|
accessToken: string
|
|
|
|
|
@ -10,7 +11,7 @@ interface AuthState {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const initialState: AuthState = {
|
|
|
|
|
accessToken: '',
|
|
|
|
|
accessToken: token.load(),
|
|
|
|
|
loading: false,
|
|
|
|
|
error: undefined,
|
|
|
|
|
}
|
|
|
|
|
@ -21,8 +22,10 @@ const authSlice = createSlice({
|
|
|
|
|
reducers: {
|
|
|
|
|
setAccessToken(state, action: PayloadAction<AuthState>) {
|
|
|
|
|
state.accessToken = action.payload.accessToken
|
|
|
|
|
token.save(state.accessToken)
|
|
|
|
|
},
|
|
|
|
|
clearToken() {
|
|
|
|
|
token.save('')
|
|
|
|
|
return initialState
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
@ -33,10 +36,10 @@ const authSlice = createSlice({
|
|
|
|
|
state.error = undefined
|
|
|
|
|
})
|
|
|
|
|
.addCase(fetchLogin.fulfilled, (state, action) => {
|
|
|
|
|
state.accessToken = String(action.payload.success?.[0]) || ''
|
|
|
|
|
state.accessToken = String(action.payload.success) || ''
|
|
|
|
|
state.loading = false
|
|
|
|
|
state.error = undefined
|
|
|
|
|
networkApi.token = state.accessToken
|
|
|
|
|
token.save(state.accessToken)
|
|
|
|
|
})
|
|
|
|
|
.addCase(fetchLogin.rejected, (state, action) => {
|
|
|
|
|
state.loading = false
|
|
|
|
|
@ -54,7 +57,7 @@ const authSlice = createSlice({
|
|
|
|
|
state.accessToken = ''
|
|
|
|
|
state.loading = false
|
|
|
|
|
state.error = undefined
|
|
|
|
|
networkApi.token = ''
|
|
|
|
|
token.save('')
|
|
|
|
|
})
|
|
|
|
|
.addCase(fetchLogout.rejected, (state, action) => {
|
|
|
|
|
state.loading = false
|
|
|
|
|
@ -64,6 +67,24 @@ const authSlice = createSlice({
|
|
|
|
|
state.error = action.error.message
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.addCase(fetchRegister.pending, state => {
|
|
|
|
|
state.loading = true
|
|
|
|
|
state.error = undefined
|
|
|
|
|
})
|
|
|
|
|
.addCase(fetchRegister.fulfilled, (state, action) => {
|
|
|
|
|
state.accessToken = String(action.payload.success) || ''
|
|
|
|
|
state.loading = false
|
|
|
|
|
state.error = undefined
|
|
|
|
|
token.save(state.accessToken)
|
|
|
|
|
})
|
|
|
|
|
.addCase(fetchRegister.rejected, (state, action) => {
|
|
|
|
|
state.loading = false
|
|
|
|
|
if (action.payload) {
|
|
|
|
|
state.error = (action.payload as ResponseData).error
|
|
|
|
|
} else {
|
|
|
|
|
state.error = action.error.message
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
selectors: {
|
|
|
|
|
accessToken: (state: AuthState) => state.accessToken,
|
|
|
|
|
@ -72,12 +93,33 @@ const authSlice = createSlice({
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
type RegisterProps = {
|
|
|
|
|
email: string
|
|
|
|
|
nickname: string
|
|
|
|
|
password: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fetchRegister = createAppAsyncThunk<ResponseData, RegisterProps>(
|
|
|
|
|
`${authSlice.name}/fetchRegister`,
|
|
|
|
|
async (props, { fulfillWithValue, rejectWithValue, extra: networkApi }) => {
|
|
|
|
|
try {
|
|
|
|
|
const data = await networkApi.request<ResponseData>('/register/', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(props),
|
|
|
|
|
})
|
|
|
|
|
return fulfillWithValue(data)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return rejectWithValue(error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type LoginProps = {
|
|
|
|
|
email: string
|
|
|
|
|
password: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const fetchLogin = createAppAsyncThunk<ResponseData, LoginProps>(
|
|
|
|
|
const fetchLogin = createAppAsyncThunk<ResponseData, LoginProps>(
|
|
|
|
|
`${authSlice.name}/fetchLogin`,
|
|
|
|
|
async (props, { fulfillWithValue, rejectWithValue, extra: networkApi }) => {
|
|
|
|
|
try {
|
|
|
|
|
@ -92,7 +134,7 @@ export const fetchLogin = createAppAsyncThunk<ResponseData, LoginProps>(
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
export const fetchLogout = createAppAsyncThunk<ResponseData>(
|
|
|
|
|
const fetchLogout = createAppAsyncThunk<ResponseData>(
|
|
|
|
|
`${authSlice.name}/fetchLogout`,
|
|
|
|
|
async (_, { fulfillWithValue, rejectWithValue, extra: networkApi }) => {
|
|
|
|
|
try {
|
|
|
|
|
@ -109,7 +151,7 @@ class Auth {
|
|
|
|
|
action = authSlice.actions
|
|
|
|
|
reducer = authSlice.reducer
|
|
|
|
|
name = authSlice.name
|
|
|
|
|
fetch = { login: fetchLogin, logout: fetchLogout }
|
|
|
|
|
fetch = { login: fetchLogin, logout: fetchLogout, register: fetchRegister }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const auth = new Auth()
|
|
|
|
|
|