Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Token Based Auth #16

Closed
amirhemm opened this issue Oct 22, 2019 · 4 comments
Closed

Token Based Auth #16

amirhemm opened this issue Oct 22, 2019 · 4 comments
Labels

Comments

@amirhemm
Copy link

Is there a way that we can use token based auth with library? I love the way you have used the new API routes in next js 9!

@hoangvvo
Copy link
Owner

Hi @amirhemm, which library are you refering too? Or is this that you are trying to implement your own token base auth? By token base, do you mean sending a token via Header?

@amirhemm
Copy link
Author

Hi @hoangvvo, for example if you want to setup Jsonwebtoken library and maybe pass the token inside of the header or inside of the cookie. Or maybe use a third party authentication service like auth0.

@hoangvvo
Copy link
Owner

@amirhemm Yes, that is certainly possible. However, the way this project set up is to use session Id. If I'm free this weekend, I will try to cook something up. Feel free to ping me in this issue this weekend if I forget.

However, a hint is to look into https://github.com/hoangvvo/nextjs-mongodb-app/blob/master/middlewares/withAuthentication.js That is our whole authentication logic. We are reading a value from session (req.session.userId). Simply do something like req.headers.authorization or .token

@hoangvvo
Copy link
Owner

This is easy to implement with the latest version with passport.

Check out /lib/passport.js. You will need to plug in passport-http-bearer

In the middleware, you want something like:

middleware.use(passport.authenticate('bearer', { session: false }))

Feel free to ask me to reopen this if you have any trouble

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants