Skip to content

[Example] Custom Auth Service that works with drf-simplejwt #2

Open
@eshaan7

Description

@eshaan7

Hi, I am using drf-simplejwt with Angular 9 for a project IntelOwl-ng.

How it works

I wrote a custom Auth.service.ts to work with simplejwt's access-refresh token pair which automatically sends refresh token request in background if access token expired but refresh token is still valid. So overall smooth and secure experience for the user.

  • This hook isAuthenticatedOrRefresh() is implemented in the HTTP interceptor as well as Auth Guard.
  • It uses localStorage by default but you can easily modify it to work with cookie storage or SessionStorage.
Code

Auth.service.ts: https://github.com/intelowlproject/IntelOwl-ng/blob/master/src/app/%40core/services/auth.service.ts

Other interesting files which leverages the use of this auth service include,

  • auth-guard.service.ts,
  • user.service.ts,
  • login.component.ts, and most importantly,
  • http.intereceptor.ts - the main problem that this interceptor class solves is that we need to filter out the /api/refresh-token and /api/login endpoints

Thought this might be useful for anyone who stumbles on this project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions