You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.
I'm having a similar issue as this one. Following the example I have this code in index.ts:
import'reflect-metadata'import'tsconfig-paths/register'importexpressfrom'express'importpassportfrom'passport'import{BearerStrategy,}from'passport-azure-ad'import{ENVIRONMENT}from'@environment'import{createConnections}from'typeorm'import{getApolloServer}from'@utils/apollo'importcorsfrom'cors'constapp=express()constbearerStrategy=newBearerStrategy({identityMetadata: 'https://login.microsoftonline.com/xxx-xxx-xxx-xxx-xxx/v2.0/.well-known/openid-configuration',clientID: 'xxx-xxx-xxx-xxx-xxx',validateIssuer: false,loggingLevel: 'info',passReqToCallback: false,},(token,done)=>{// Send user info using the second argumentdone(null,{},token)})app.use(cors({origin: true}))app.use(passport.initialize())passport.use(bearerStrategy)app.use(passport.authenticate('oauth-bearer',{session: false}),(req,res,next)=>{console.log('User info: ',req.user)console.log('Validated claims: ',req.authInfo)next()});(async()=>{try{awaitcreateConnections()constserver=awaitgetApolloServer()server.applyMiddleware({ app,cors: false})app.listen({port: ENVIRONMENT.port},()=>{console.log(`Server ready at http://localhost:${ENVIRONMENT.port}${server.graphqlPath}`)}).on('error',function(error){throw`Failed starting Express server on port ${ENVIRONMENT.port}: ${error}`})}catch(error){console.error(`Failed starting server: ${error}`);process.exit(1)}})()
Notice that he req.user is undefined. According to the documentation I would expect passport to set this for us? Or am I missing something here? To store the user in the database we should probably use the field oid as unique identifier?
@DarkLite1 sorry this went unattended. So the req.user attribute is usually populated with session information about an authenticated user, but sessions are not common/appropriate for web API's, as API servers typically require credentials (i.e. token) to be supplied with each request. As such, our sample is not covering this.
If you have some custom logic for populating req.user after an authorized call, you could do so at line 18 in index.js:
constbearerStrategy=newBearerStrategy(config,function(token,done){// Send user info using the second argumentdone(null,{/* USER INFO */},token);});
But for sessions you should take a look at a web app sample. You can see some such samples here.
I'm having a similar issue as this one. Following the example I have this code in
index.ts
:Which results in these console logs:
Notice that he
req.user
is undefined. According to the documentation I would expectpassport
to set this for us? Or am I missing something here? To store the user in the database we should probably use the fieldoid
as unique identifier?A similar question on StackOverlflow.
Thank you for your help.
The text was updated successfully, but these errors were encountered: