adonisjs / auth

Official Authentication package for AdonisJS
https://docs.adonisjs.com/guides/auth/introduction
MIT License
191 stars 65 forks source link

[V5] API Token migration date is invalid #165

Closed smallbatch-apps closed 3 years ago

smallbatch-apps commented 3 years ago

I started off by creating my basic models and relationships, including the users table. I then followed the documentation to add authentication.

The Auth Migration that is created by the invoke @adonisjs/auth command is created with the incorrect date. Some investigation shows that this is coming from adonisjs/auth/instructions.ts.

There is a hard coded string value given the dates on lines 37 & 38.

const USER_MIGRATION_TIME_PREFIX = '1587988332388'
const TOKENS_MIGRATION_TIME_PREFIX = '1592489784670'

These hard-coded dates mean that if you use this invoke command to create the token table but NOT the users table you end up with invalid migrations.

To be honest I fail to see why these are hard coded at all. It's not necessary, and arguably incorrect. The migration is run at the time the migration is run - ${Date.now()}_${state.tokensTableName}.ts on line seems to be a far more reasonable solution.

Package version

Unclear - whatever node ace executes.

Node.js and npm version

Node: v12.19.0 NPM: 6.14.8

Sample Code

This should be reproducible by simply doing the following - assuming a new Adonis application has been created.

node ace make:model User -m
npm i @adonisjs/auth@alpha
node ace invoke @adonisjs/auth // select yes to creating migration

Note that the following migrations were created:

1592489784670_api_tokens.ts
1617611671407_users.ts

Given that the migrations will be run in order, this will undoubtedly try to run out of order, adding a foreign key onto a table that does not exist. And indeed:

[ error ]  alter table "api_tokens" add constraint "api_tokens_user_id_foreign" foreign key ("user_id") references "users" ("id") on delete CASCADE - relation "users" does not exist
thetutlage commented 3 years ago

Yup. Sounds reasonable. Feel free to create a PR to fix it