oslabs-beta / onyx

Onyx is authentication middleware for Deno, inspired by Passport.js
http://deno.land/x/onyx@v1.0.1
MIT License
160 stars 10 forks source link

Problems trying to get failure and success messages #33

Open decosvaldo opened 3 years ago

decosvaldo commented 3 years ago

Hi everybody, I'm trying Onyx on my new Deno application. Thanks for the effort!

I'm using the "info" field to set information about authentication error. I was using it with Node/Passport so I can return an accurate message for the user. The thing is I don't know how to get this information after my authentication proccess.

If I try to define the options here: await (await onyx.authenticate('local',{ successMessage:"success test", failureMessage:"failure test"}))(ctx)

I receive this error:

error: Uncaught (in promise) TypeError: Cannot read property 'message' of undefined
        if (!context.state.onyx.session.message) {

My strategy is bellow:

onyx.use(
    new LocalStrategy(
        async (username: string, password: string, done: Function) => {
            //const { username, password } = context.state.onyx.user;
            const email = username
            //console.log(
            //    `verify function invoked with username ${username} and password ${password}`
            //);
            if(email && password) {
                const user = await loadUser(email)
                if(user.length === 1){
                    if(user[0].status == 2){
                        await done(null, false, "Sua conta está bloqueada. Se você não concorda com este comportamento, por favor entre em contato conosco.")
                    }else if(user[0].useGoogle == 1){
                        await done(null, false, "Você optou por se autenticar com a sua conta Google, portanto não é possível utilizar este formulário de autenticação.")
                    }else if(user[0].status == 1){
                        const authenticated = await bcrypt.compare(password, user[0].password)
                        if(authenticated){
                            user[0].password = ""
                            await done(null, user[0], "OK")
                        }else{
                            await done(null, false, "Usuário ou senha incorretos.")
                        }
                    }else if(user[0].status == 3){
                        await done(null, false, "Você ainda não ativou a sua conta, caso não tenha recebido o e-mail de ativação, por favor entre em contato conosco.")
                    }
                }
            }
            await done(null, false, "Não foi possível autenticar. Suas informações estão corretas? Se você acredita que sim e mesmo assim não consegue, entre em contato conosco.")
        }
    )
)

I want to use the text information sent on the third parameter of the done function and display it to the user after the await (await onyx.authenticate('local'))(ctx) call.

I'm not an expert so, what I am doing wrong?

Also, do you know when oauth strategies like Google will be available? Thanks

decosvaldo commented 3 years ago

Don't know if this is the right place, but I got the messages after editing local-strategy.ts's verified function to:

async function verified(err: any, user?: any, info?: any) {
      if (err) {
        context.state.onyx.failureMessage = err //new
        return self.funcs.error(err);
      }
      if (!user) {
        context.state.onyx.failureMessage = info //new
        return self.funcs.fail(info);
      }
      context.state.onyx.successMessage = info //new
      await self.funcs.success(user, info);
    }

Is this the right place to do it?