Playing Next Lesson In
seconds

Let's Learn AdonisJS 6 #7.5

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.

Created by
@tomgobich
Published

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

node ace make:migration remember_me_tokens
Copied!

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

Create a free account to join in on the discussion
  1. 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
    1. Responding to vladimir-laskin
      @tomgobich

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

      0