Implementation auth for jwt

#1

hi dears, i’d like if some body have some project of provider for generate the controller for auth, I do it and insert in my fork, but It was not accepted, then I think in make a provider or command line for generate it like in laravel

my fork

0 Likes

#2

I recommend using Adonis persona. This can work with traditional session auth and with JWT too

0 Likes

#3

Persona seems very cool. When I finally have it working (and my head is no longer bloody from beating it on my keyboard) I’m going to write up tutorial with all the things we’re all going to have to do to use it.

Marty

1 Like

#4

Looking forward to your tutorial!

0 Likes

#5

Hi! I use Adonisjs in “api only” mode with vuejs frontend. Is persona good for this case? And how to use JWT with it?

0 Likes

#6

Hi Xia!

I use Adonis API only for backend and Nuxt for frontend as two separate apps (not AdoNuxt)

Works well with Persona and JWT. All I had to do was to change login little bit.

Persona Doc states under what it does not do:
3. Does not create sessions or generate JWT tokens.

So you have to generate JWT on your own after login attempt

// Let Persona handle login attempt
async login ({ request, auth }) {
  const payload = request.only(['uid', 'password'])
  // Throws error if invalid payload
  const user = await Persona.verify(payload) 
  // If login is OK, then generate token
  return auth.generate(user)
}

auth.generate() doc

Other way is to roll your own login

// Let auth handle login attempt
async login ({ request, auth }) {
  let { uid, password } = request.all()
  return auth.attempt(uid, password)
}

auth.attempt() doc

1 Like

#7

Did you succeed to send emails with Persona @McSneaky ?

0 Likes

#8

Hi @begueradj

I started implementing Persona ~30 minutes ago.

But looking at events Persona emits I think I can send email template to Mailgun to send it out actually

Have not tested so far, but here is my idea:

Create new event listener:
https://adonisjs.com/docs/4.1/events#_events_overview
adonis make:listener User

Then inside generated file will write something like this:
https://adonisjs.com/docs/4.1/mail#_basic_example

const Mail = use('Mail')

Event.on('user::created', async (user) => {
  await Mail.send('emails.welcome', user.toJSON(), (message) => {
      message
        .to(user.email)
        .from('<from-email>')
        .subject('Welcome to yardstick')
    })
})
1 Like