Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@vouill
vouill / app.js
Created April 29, 2019 08:32
Preparing SQL queries
// before
db.all(`SELECT * FROM POSTS WHERE ID = ${req.params.id};`, function(
err,
row
) {
res.send(row);
});
// after
@vouill
vouill / app.js
Created December 4, 2017 11:19
app.vue mehtod handling unauthorize
created: function () {
axios.interceptors.response.use(undefined, function (err) {
return new Promise(function (resolve, reject) {
if (err.status === 401 && err.config && !err.config.__isRetryRequest) {
// if you ever get an unauthorized, logout the user
this.$store.dispatch(AUTH_LOGOUT)
// you can also redirect to /login if needed !
}
throw err;
});
@vouill
vouill / main.js
Created December 4, 2017 11:16
auth main
const token = localStorage.getItem('user-token')
if (token) {
axios.defaults.headers.common['Authorization'] = token
}
@vouill
vouill / router.js
Last active January 30, 2018 09:54
router auth
import store from '../store' // your vuex store
const ifNotAuthenticated = (to, from, next) => {
if (!store.getters.isAuthenticated) {
next()
return
}
next('/')
}
@vouill
vouill / auth.module.js
Last active January 18, 2018 13:55
token usage
const actions = {
[AUTH_REQUEST]: ({commit, dispatch}, user) => {
return new Promise((resolve, reject) => {
commit(AUTH_REQUEST)
axios({url: 'auth', data: user, method: 'POST' })
.then(resp => {
const token = resp.data.token
localStorage.setItem('user-token', token)
// Add the following line:
axios.defaults.headers.common['Authorization'] = token
@vouill
vouill / logout.js
Created December 4, 2017 11:05
logout method
methods: {
logout: function () {
this.$store.dispatch(AUTH_LOGOUT)
.then(() => {
this.$router.push('/login')
})
}
},
@vouill
vouill / logout.js
Last active December 4, 2017 11:06
auth logout
const actions = {
...
[AUTH_LOGOUT]: ({commit, dispatch}) => {
return new Promise((resolve, reject) => {
commit(AUTH_LOGOUT)
localStorage.removeItem('user-token') // clear your user's token from localstorage
resolve()
})
@vouill
vouill / auth.module.js
Last active December 4, 2017 11:11
auth mutations
// basic mutations, showing loading, success, error to reflect the api call status and the token when loaded
const mutations = {
[AUTH_REQUEST]: (state) => {
state.status = 'loading'
},
[AUTH_SUCCESS]: (state, token) => {
state.status = 'success'
state.token = token
},
@vouill
vouill / auth.module.js
Last active January 18, 2018 13:55
Actions
const actions = {
[AUTH_REQUEST]: ({commit, dispatch}, user) => {
return new Promise((resolve, reject) => { // The Promise used for router redirect in login
commit(AUTH_REQUEST)
axios({url: 'auth', data: user, method: 'POST' })
.then(resp => {
const token = resp.data.token
localStorage.setItem('user-token', token) // store the token in localstorage
commit(AUTH_SUCCESS, token)
// you have your token, now log in your user :)
@vouill
vouill / auth.module.js
Created December 4, 2017 10:57
Getters
const getters = {
isAuthenticated: state => !!state.token,
authStatus: state => state.status,
}