-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
251 additions
and
169 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import axios from 'axios'; | ||
|
||
const Auth = { | ||
doLogin: async function(username, password) { | ||
try{ | ||
let res = await axios.post('/api/auth/login', { | ||
username: username, | ||
password: password | ||
}); | ||
if (res.data.success){ | ||
localStorage.setItem('jwt-token', res.data.token); | ||
localStorage.setItem('userdata', JSON.stringify(res.data.userdata)); | ||
return null; | ||
}else{ | ||
return res.data.error; | ||
} | ||
} catch (err) { | ||
return 'connection'; | ||
} | ||
}, | ||
|
||
doLogout: () => { | ||
localStorage.removeItem('jwt-token'); | ||
localStorage.removeItem('userdata'); | ||
}, | ||
|
||
validateUser: async function() { | ||
let res = await axios.get('/api/auth/user', Auth.getHeader()); | ||
if (res.data){ | ||
localStorage.setItem('userdata', JSON.stringify(res.data)); | ||
}else{ | ||
localStorage.removeItem('jwt-token'); | ||
localStorage.removeItem('userdata'); | ||
} | ||
return res; | ||
}, | ||
|
||
getUser: () => { | ||
try{ | ||
if (localStorage.getItem('jwt-token')) | ||
return JSON.parse(localStorage.getItem('userdata') || 'null'); | ||
return null; | ||
}catch(err){ | ||
localStorage.removeItem('jwt-token'); | ||
localStorage.removeItem('userdata'); | ||
return null; | ||
} | ||
}, | ||
|
||
getHeader: () => ({headers: {'x-access-token': localStorage.getItem('jwt-token')}}) | ||
}; | ||
|
||
export default Auth; |
This file contains 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
Oops, something went wrong.