summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/auth/auth.actions.js
diff options
context:
space:
mode:
Diffstat (limited to 'animism-align/frontend/app/views/auth/auth.actions.js')
-rw-r--r--animism-align/frontend/app/views/auth/auth.actions.js26
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 })
}