Package: drf-restauth
drf-authPainless token authentication for django restframework. Built on top of rest_framework.auth_token. It's meant to provide a ready to use authentication for yourSPAsand otherMobile AppsInstallationpipinstalldrf-restauthHomepageThe project homepage on:GithubUsageINSTALLED_APPS=['rest_framework','rest_framework.authtoken','drf_auth']Configure project urls.py:Subsequent examples assume, you are using "/api/auth/ as the path prefix.urlpatterns=[path("api/auth/",include("drf_auth.urls"))]# settings.pyREST_FRAMEWORK={'DEFAULT_RENDERER_CLASSES':['rest_framework.renderers.JSONRenderer',],'DEFAULT_AUTHENTICATION_CLASSES':['rest_framework.authentication.TokenAuthentication'],'DEFAULT_PERMISSION_CLASSES':['rest_framework.permissions.IsAuthenticated']}# drf-specific settings for password resetDRF_AUTH_SETTINGS={"SITE_NAME":"My Site Title","PASSWORD_RESET_REDIRCT_URL":"/","PASSWORD_CHANGE_TEMPLATE":"drf_auth/password_change_form.html","EMAIL_HOST_USER":"youremail@gmail.com","EMAIL_HOST_PASSWORD":"yourpassword","EMAIL_HOST":"smtp.gmail.com","EMAIL_PORT":587,}These settings can be ignored if you don't plan to do password reset by email!Endpoints:/POST api/auth/register/{"username":"string","password":"string","email":"string","first_name":"string","last_name":"string"}response:{"token":"string","user":{"username":"string","password":"string","email":"string","first_name":"string","last_name":"string"}}/POST api/auth/login/body:{"username":"string","password":"string"}response:{"token":"string","user":{"username":"string","password":"string","email":"string","first_name":"string","last_name":"string"}}/POST api/auth/logout/body: null
response:{
    "success": true
}/GET api/auth/user/ (Protected Route)response:{"username":"string","password":"string","email":"string","first_name":"string","last_name":"string"}GET /api/auth/users (Protected route, must be admin)Retrieves a json array of all users unpaginated/api/auth/update-user/ (Protected route)body:{"email":"string","first_name":"string","last_name":"string"}response:{"username":"string","password":"string","email":"string","first_name":"string","last_name":"string"}POST /api/auth/change-password/ (Protected route)body:{"old_password":"string","new_password":"string",}response:{"username":"string","password":"string","email":"string","first_name":"string","last_name":"string"}POST /api/auth/reset-password/For restting forgotten passwords. An email will be sent
using the settings provided in settings.DRF_AUTH_SETTINGS
dictionary.body:{"email":"string",}status:200-OK(Emailsent)status:400-Emailnotsentstatus:500-Internalservererrorresponse:{"message":"string"}Handle user email confirmationThis route handles navigations/get requests when the user clicks the password reset link.For a complete workflow, provide a template to render in DRF_AUTH_SETTINGS(see above) and make sure that
the new password isPOSTEDto the same route.The following variables are passed to you in the context for customization:usersite_name/POST /api/auth/reset_password_confirmation/<uidb64>/<token>/Note that the token expires after 30 minutes after the email is sentbody:{"password":"string"}Required HeadersAuthorization: Token xxxxxxxx (required for protected routes)Content-Type: application/jsonX-Requested-With: XMLHttpRequest (Desirable)Practical examples using typescriptimportaxiosfrom"axios";// Add content-type header on every requestaxios.interceptors.request.use(function(config){consttoken=localStorage.getItem("token");if(token){config.headers.Authorization=`Token${token}`;}config.headers["Content-Type"]="application/json";returnconfig;});consthandleLogin=async(username:string,password:string)=>{constbody=JSON.stringify({username,password});constres=awaitaxios.post("/api/auth/login/",body);const{user,token}=res.data;localStorage.setItem("token",token);localStorage.setItem("user",JSON.stringify(user));}interfaceUser{username:string,first_name:string,last_name:string,password:string,email:string}consthandleRegister=async(user:User):Promise<User>=>{constbody=JSON.stringify(user);constres=awaitaxios.post("/api/auth/login/",body);const{user,token}=res.data;localStorage.setItem("token",token);localStorage.setItem("user",JSON.stringify(user));returnuser}typeLogoutResponse={success:boolean}consthandleLogout=():Promise<LogoutResponse>=>{constres=awaitaxios.post("/api/auth/logout/",null)returnres.data}constgetLoggedInUser=():Promise<User>=>{constres=awaitaxios.get("/api/auth/user/")returnres.data}Submit an issue atGithubFeel free to add your voice but be gentle, this is my first open source Django package!