passportjs | Generating Sign-In and Sign-Out authentication strategies | Authentication library
kandi X-RAY | passportjs Summary
kandi X-RAY | passportjs Summary
Generating Sign-In and Sign-Out authentication strategies using Passport.js.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of passportjs
passportjs Key Features
passportjs Examples and Code Snippets
Community Discussions
Trending Discussions on passportjs
QUESTION
I am a beginner to nodejs and I am creating my web app. I use passportJs for authentication. As it is mentioned in the documentation that when the user is successfully authenticated, req.user will be created and can be accessed in any route.
My admin.handlebars
...ANSWER
Answered 2021-Jun-09 at 10:29End-user(in your case hacker) can add any type of data to any request. So yes, end-user can modify requests to send req.user within it. However, they won't be able to access the data within it and their request will not be accepted on your "admin" endpoint if you use req.isAuthenticated()
.
This is because passport JS serialises the user and stores the information in session after encryption. So UNLESS the end-user (Hacker) has access to another user's machine and copies all the session details (Browser's don't allow other sites to access another sites session) from their browser and use it, they won't be able to use admin.
TLDR;
No they wont be able to access "admin" endpoint by simply adding req.user in their request.
QUESTION
I have a few questions regarding how passport.js works. On it's documentation theres an axample:
...ANSWER
Answered 2021-May-30 at 20:15You pass your done()
function as an argument when you call passport.authenticate()
. The time you call it, your strategy is called too and passes the function you defined as 3d argument.
Visual Flow
QUESTION
So I've been trying to write an authentication system on my VueJs app with PassportJs
. I wrote a function to initialize the basics of PassportJs
that looks like this:
ANSWER
Answered 2021-May-23 at 10:59Take a look at passport
documentation for the authenticate()
method.
When using a custom callback, it becomes the application's responsibility to establish a session (by calling
req.login()
) and send a response.
So in your first scenario, you need to explictly establish a session - passport will not do it automatically for you.
Regarding your second scenario where no custom callback is used:
By default, if authentication fails, Passport will respond with a 401 Unauthorized status, and any additional route handlers will not be invoked.
As far as I understand, with this setup, flash messages set up as part of the Strategy
verify callback are not being used - unless redirect
options are specified, which you do not want here.
So you probably should go with the first scenario in order to be able to customize error responses with the Strategy
verify callback flash messages. Just make sure you establish the session when login is successful.
http://www.passportjs.org/docs/authenticate/
Edit: I've just ran through a quick test, and it seems that alternatively you should be able to get away with just having the Strategy
verify callback return errors with the error.message
property set to whatever error message you want to send as a response - without the need for the custom callback and flash messages at all. Note though that in this case passport actually sets the response body to the error message as text.
So for instance instead of:
QUESTION
I am working on implementing custom error handling in a MongoDB MERN application. I’m also using Mongoose and passport-local-mongoose. When a user registers for a new account, they should get a specific error message in the chrome console saying, ‘username already exists’. I know it is a bad idea to show all the server error messages to the user, but I would like to show a select few.
Links that I have tried: https://github.com/saintedlama/passport-local-mongoose
Passport-local-mongoose : Authenticate user right after registration
https://www.geeksforgeeks.org/nodejs-authentication-using-passportjs-and-passport-local-mongoose/
Here is the code: server/controller/auth.js
...ANSWER
Answered 2021-May-05 at 18:04- When you send a status code that is not in the range 200, it is considered as an "exception" in the client code. For Axios specifically, the
catch
block is executed. In your code, it is
QUESTION
I've tried to check if they're online examples of how to use JWT extractors
to get the token from the request but I failed to understand how to send the token
with the request after the user logins.
When I use Postman, there's a tab called Authorization
where I can choose the type Bearer Token
which enabled me to add the token
with the Authorization
and the request http://localhost:5000/profile
went successfully.
However, the browser stills showing me only Unauthorized
when I try to access the profile http://localhost:5000/profile
after successful login.
POSTMAN SCREEN-SHOT:
BROWSER SCREEN-SHOT:
I've followed the passpot-jwt documentation configuration:
...ANSWER
Answered 2021-May-01 at 12:03The issue is:
I was trying to access the profile
without adding the Authorization
in the header
from the server itself. The Authorization
contains the generated token
.
With Postman I was able to do that with the UI as explained above. However, in the code, I needed to create a middleware before accessing the profile
route.
QUESTION
I am going to build a web application that allows users to sign in with their Google or Twitter account. I think OpenID Connect(OAuth2) is the standard today to verify the identity. I also want to provide several API services that can be only accessed with a valid access token from either Google or Twitter.
For example, all the four API's above are going to be public and so I have to protect from unauthorized users. For NodeJS based API services I can use http://www.passportjs.org/ to protect all APIs.
Assume, in the future the number of API's will be grow for example up to 20 API's and sign in with Facebook account will be also allowed. Again, all the API's have to be protected and I have to do it 16 times with http://www.passportjs.org/. In addition add the new provider Facebook, I have to do the changes on all 20 APIs.
The question is, is their a way to keep centralized, which means in the future when I will provide more the providers for example GITHUB for sign in I would like to do changes in one place not in 20 places. Is the tool https://www.ory.sh/hydra what I need?
...ANSWER
Answered 2021-Apr-22 at 15:06These are perhaps the two primary features of OAuth 2.0 and Open ID Connect:
Federated sign in to your UIs via multiple identity providers and the ability to easily add new options such as GitHub in a centralised manner
Full control over claims included in access tokens, so that your APIs can authorize requests however you'd like
FOREIGN ACCESS TOKENS
You should aim to avoid ever using these in your apps. Your UIs and APIs should only use tokens issued by your own Authorization Server (Ory Hydra), which manages the connection to the Identity Provider. Adding a new sign in method will then just involve centralised configuration changes, with zero code changes in either UIs or APIs.
IF YOU DON'T HAVE AN AUTHORIZATION SERVER YET
Maybe have a look at the Curity Identity Server and its free community edition - use sign in with GitHub, which has strong support for both of these areas:
EXTERNAL RESOURCES
One exception to the above is that your APIs may occasionally need to access a user's Google resources after login, by calling Google APIs. This would require the token issued by Google. It can be managed via an embedded token approach - though it doesn't sounds like you need that right now.
QUESTION
I am using passportjs middleware for authentication in an express/nodejs application. I am getting errors about properties on my request.user object despite having followed the steps for Declaration Merging.
I have created a file at /types/index.d.ts in my project root, and added the following to my tsconfig.json
...ANSWER
Answered 2021-Apr-13 at 16:02To extend the User
type used by Passport, you would merge your declarations into global.Express.User
:
QUESTION
I recently set up Caddy to reverse proxy all my services on the internet. In order to keep everything organized I started using for the first time NodeJS to render a custom homepage with links to all the services. The problem is that I want to implement a login sistem in node at the level of the homepage and authenticate the user so that he can connect to all the services proxied by Caddy.
My idea is to implement a cookies-based login sistem and update constantly Caddy so that for every endpoint of the site only the user carring a valid authorization cookie can access.
I looked online on how to create a login system and found some resources using passportjs and local strategy, from which I do not know how to create tokens and authorization coockies. I found a library called passport-cookie but again I would need a list of authorized cookie at every login to feed it to Caddy and secure also endpoints not controlled by node. How could I do this?
...ANSWER
Answered 2021-Apr-13 at 11:58There are a few options to achieve your goal:
- Basic auth
The simplest solution to secure your services would be Basic Auth, which basically tells your user's browser to ask for a username and password which is then sent to the server.
https://caddyserver.com/docs/caddyfile/directives/basicauth
Even though it is very quick to set up, you lose benefits such as fine-grined access control and your users' ability to change their username/password.
- OAuth
OAuth allows your users to sign in with their own account, for example from Google or Facebook. Take a look at this complete Caddy Auth System: https://github.com/greenpau/caddy-auth-portal
- JWT - Build your own
Finally, if you want to use this challenge as a learning opportunity, you can take a look at JWT based authentication. Take a look at this module: https://github.com/greenpau/caddy-auth-jwt
This would enable you to issue JWT tokens in your node.js application which are then verified by Caddy.
Obviously, all of these solutions do require some research and skill and I would rate their difficulty to implement in ascending order, with your plan of building your own Auth system being the most difficult.
QUESTION
I am using passportJS for authentication. I have called a function setAuthenticatedUser in app.js
...ANSWER
Answered 2021-Mar-05 at 05:57Try flipping the order of the last two calls to app.use
. Express will call those functions in order as defined.
I'm assuming your routes defined in ./routes
don't call next()
, so there's no way for the next set of middlewares (in this case passport.setAuthenticatedUser
) to run. So make sure that this call to the Passport function happens before your route handler, or more generally, before any function that won't call next()
.
QUESTION
As per the passportjs documentation, the verifier function is supposed to be called. But I am not seeing this call back.
I am testing this with okta. In okta the app is configured to send the saml response to /fromokta The okta is redirecting to /fromokta What am I missing?
...ANSWER
Answered 2021-Feb-15 at 16:42This most likely means your POST /login route never gets the request. Look at the network requests and see if there is indeed a POST /login to the URL, make sure the protocol is the same too (http and https)
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install passportjs
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page