Skip to content

Instantly share code, notes, and snippets.

@eblind39
Last active August 14, 2022 01:54
Show Gist options
  • Save eblind39/5015a862e1fd228c89abe7c7be5d1ff8 to your computer and use it in GitHub Desktop.
Save eblind39/5015a862e1fd228c89abe7c7be5d1ff8 to your computer and use it in GitHub Desktop.
Redux toolkit, do signIn via thunk & save JWT in cookie
export const signIn = createAsyncThunk(
'authentication/signin',
async ({email, password}: thunkArgs) => {
const apiURL = `${API_URL}/authentication/signin`
const strBody: BodyInit = `{"email":"${email}","password":"${password}"}`
const response = await fetch(apiURL, {
method: 'POST',
mode: 'cors',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: strBody,
})
if (response.ok) {
document.cookie = response.headers.get('my-response-header-jwt') || ''
const data: UserRole = (await response.json()) as UserRole
return [data]
}
return []
},
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment