Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 22 additions & 18 deletions src/lib/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@/store/mutation-types.js'

let channel
const AUTHENTICATED_REQUEST_TIMEOUT_MS = 10000

const auth = {
logIn(payload, callback) {
Expand Down Expand Up @@ -90,24 +91,27 @@ const auth = {
},

isServerLoggedIn(callback) {
superagent.get('/api/auth/authenticated').end((err, res) => {
if (err && res && [401, 422].includes(res.statusCode)) {
store.commit(USER_LOGIN_FAIL)
callback(null)
} else if (err) {
store.commit(USER_LOGIN_FAIL)
callback(err)
} else if (res && res.body === null) {
store.commit(USER_LOGIN_FAIL)
callback(err)
} else {
const user = res.body.user
const organisation = res.body.organisation || {}
store.commit(SET_ORGANISATION, organisation)
store.commit(USER_LOGIN, user)
callback(null)
}
})
superagent
.get('/api/auth/authenticated')
.timeout(AUTHENTICATED_REQUEST_TIMEOUT_MS)
.end((err, res) => {
if (err && res && [401, 422].includes(res.statusCode)) {
store.commit(USER_LOGIN_FAIL)
callback(null)
} else if (err) {
store.commit(USER_LOGIN_FAIL)
callback(err)
} else if (res && res.body === null) {
store.commit(USER_LOGIN_FAIL)
callback(err)
} else {
const user = res.body.user
const organisation = res.body.organisation || {}
store.commit(SET_ORGANISATION, organisation)
store.commit(USER_LOGIN, user)
callback(null)
}
})
},

// Needed for router to know if a redirection to login page is required or
Expand Down