from django.contrib import admin
from django.urls import path
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView)
from DjangoRestAuth.views import ModsView
urlpatterns = [
path(‘admin/’, admin.site.urls),
# Path to obtain a new access and refresh token
path(‘api/token/’, TokenObtainPairView.as_view(), name=‘token_obtain_pair’),
# Submit your refresh token to this path to obtain a new access token
path(‘api/token/refresh/’, TokenRefreshView.as_view(), name=‘token_refresh’),
# Return ‘Mods’ model objects
path(‘mods/’, ModsView.as_view(), name=‘mods_view’)
]
import Vue from 'vue'
import Vuex from 'vuex'
import { axiosBase } from './api/axios-base'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
accessToken: localStorage.getItem('access_token') || null, // makes sure the user is logged in even after
// refreshing the page
refreshToken: localStorage.getItem('refresh_token') || null,
APIData: '' // received data from the backend API is stored here.
},
getters: {
loggedIn (state) {
return state.accessToken != null
}
},
mutations: {
updateLocalStorage (state, { access, refresh }) {
localStorage.setItem('access_token', access)
localStorage.setItem('refresh_token', refresh)
state.accessToken = access
state.refreshToken = refresh
},
updateAccess (state, access) {
state.accessToken = access
},
destroyToken (state) {
state.accessToken = null
state.refreshToken = null
}
},
actions: {
// run the below action to get a new access token on expiration
refreshToken (context) {
return new Promise((resolve, reject) => {
axiosBase.post('/api/token/refresh/', {
refresh: context.state.refreshToken
}) // send the stored refresh token to the backend API
.then(response => { // if API sends back new access and refresh token update the store
console.log('New access successfully generated')
context.commit('updateAccess', response.data.access)
resolve(response.data.access)
})
.catch(err => {
console.log('error in refreshToken Task')
reject(err) // error generating new access and refresh token because refresh token has expired
})
})
},
registerUser (context, data) {
return new Promise((resolve, reject) => {
axiosBase.post('/register', {
name: data.name,
email: data.email,
username: data.username,
password: data.password,
confirm: data.confirm
})
.then(response => {
resolve(response)
})
.catch(error => {
reject(error)
})
})
},
logoutUser (context) {
if (context.getters.loggedIn) {
return new Promise((resolve, reject) => {
axiosBase.post('/api/token/logout/')
.then(response => {
localStorage.removeItem('access_token')
localStorage.removeItem('refresh_token')
context.commit('destroyToken')
})
.catch(err => {
localStorage.removeItem('access_token')
localStorage.removeItem('refresh_token')
context.commit('destroyToken')
resolve(err)
})
})
}
},
loginUser (context, credentials) {
return new Promise((resolve, reject) => {
// send the username and password to the backend API:
axiosBase.post('/api/token/', {
username: credentials.username,
password: credentials.password
})
//if successful update local storage:
.then(response => {
context.commit('updateLocalStorage', { access: response.data.access, refresh: response.data.refresh }) // store the access and refresh token in localstorage
resolve()
})
.catch(err => {
reject(err)
})
})
}
}
})
from django.urls import path
from rest_framework.authtoken.views import obtain_auth_token # <-- Here
from myapi.core import views
urlpatterns = [
path(‘hello/’, views.HelloView.as_view(), name=‘hello’),
path(‘api-token-auth/’, obtain_auth_token, name=‘api_token_auth’), # <-- And here
]
get_userinfo
- id
- username
- is_superuser
- is_staff
- is_active
- date_joined
- last_login
- data (token,regoin_code)
- …