summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/auth/auth.actions.js
blob: d7663b746cd30e7c40c6e30f945d252a2b22cf36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import fetch from 'node-fetch'

import { session } from 'app/session'

const urls = {
  login: "/api/v1/auth/login",
}

export const login = (data) => dispatch => (
  fetch(urls.login, {
    method: 'POST',
    body: JSON.stringify(data),
    credentials: 'same-origin',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    }
  })
  .then(req => req.json())
  .then(res => {
    if (res.access_token) {
      session.set('access_token', res.access_token)
    }
    return res
  })
  .catch(error => {
    console.error(error)
  })
)

export const logout = () => dispatch => {
  session.set('access_token', '')
}