Remembering A User's Authenticated Session

In this lesson, we'll learn how we can use AdonisJS' Remember Me Tokens feature to allow a user to specify they'd like their authentication state to be remembered for a long time across sessions.

Published
Apr 24, 24
Duration
6m 55s

Developer & dog lover. I teach AdonisJS, a full-featured Node.js framework, at Adocasts where I publish weekly lessons. Professionally, I work with JavaScript, .NET, and SQL Server.

Adocasts

Burlington, KY

Get the Code

Download or explore the source code for this lesson on GitHub

Repository

You can use the below command to create your remember me tokens migration.

node ace make:migration remember_me_tokens

Then, use the below to define your migration

import { BaseSchema } from '@adonisjs/lucid/schema'

export default class extends BaseSchema {
  protected tableName = 'remember_me_tokens'

  async up() {
    this.schema.createTable(this.tableName, (table) => {
      table.increments()
      table
        .integer('tokenable_id')
        .notNullable()
        .unsigned()
        .references('id')
        .inTable('users')
        .onDelete('CASCADE')

      table.string('hash').notNullable().unique()
      table.timestamp('created_at').notNullable()
      table.timestamp('updated_at').notNullable()
      table.timestamp('expires_at').notNullable()
    })
  }

  async down() {
    this.schema.dropTable(this.tableName)
  }
}
Copied!

You can also grab this from the documentation as well.

Join The Discussion! (2 Comments)

Please sign in or sign up for free to join in on the dicussion.

  1. Commented 2 months ago

    This episode is such a mind blow.

    I have rolled out custom JWT with refresh and access tokens for an Express backend, and it took me 2 weeks (+ so many corrections through its lifetime) to do something that you have done in Adonis in less than 7 minutes.

    Absolutely insane

    1

    Please sign in or sign up for free to reply

    1. Commented 2 months ago

      Happy to hear you enjoyed, Vladimir!! JWT & refresh tokens can definitely be complicated to implement, for sure! 😊

      0

      Please sign in or sign up for free to reply