Last active
August 14, 2022 01:54
-
-
Save eblind39/5015a862e1fd228c89abe7c7be5d1ff8 to your computer and use it in GitHub Desktop.
Redux toolkit, do signIn via thunk & save JWT in cookie
This file contains hidden or 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
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