diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2021-03-05 21:34:34 +0100 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2021-03-05 21:34:34 +0100 |
| commit | 88d092e21a4ea296ce804ef416683807df4b7d38 (patch) | |
| tree | 53e85f1e415cfc298f814d52029516c1a33de712 /animism-align/frontend/app/views/auth/auth.actions.js | |
| parent | 0907418ce2c6ca498b02e8e514e4945d79750467 (diff) | |
change password. log in
Diffstat (limited to 'animism-align/frontend/app/views/auth/auth.actions.js')
| -rw-r--r-- | animism-align/frontend/app/views/auth/auth.actions.js | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/animism-align/frontend/app/views/auth/auth.actions.js b/animism-align/frontend/app/views/auth/auth.actions.js index d7663b7..936e062 100644 --- a/animism-align/frontend/app/views/auth/auth.actions.js +++ b/animism-align/frontend/app/views/auth/auth.actions.js @@ -1,12 +1,14 @@ import fetch from 'node-fetch' +import jsonwebtoken from 'jsonwebtoken' +import * as types from 'app/types' import { session } from 'app/session' const urls = { login: "/api/v1/auth/login", } -export const login = (data) => dispatch => ( +export const login = data => dispatch => ( fetch(urls.login, { method: 'POST', body: JSON.stringify(data), @@ -18,16 +20,26 @@ export const login = (data) => dispatch => ( }) .then(req => req.json()) .then(res => { - if (res.access_token) { - session.set('access_token', res.access_token) + if (!res.access_token) { + throw new Error(res.description) } - return res - }) - .catch(error => { - console.error(error) + session.set('access_token', res.access_token) + load_access_token()(dispatch) }) ) +export const load_access_token = () => dispatch => { + const access_token = session.get('access_token') || null + if (access_token) { + const creds = jsonwebtoken.decode(access_token) + const user_id = creds.identity + return dispatch({ type: types.auth.logged_in, user_id }) + } else { + return dispatch({ type: types.auth.logged_out }) + } +} + export const logout = () => dispatch => { session.set('access_token', '') + return dispatch({ type: types.auth.logged_out }) } |
