TypeScript Type Mismatch in Microsoft Authentication with Passport

I've created a backend using Microsoft authentication in Express. I'm facing some type errors that are a bit confusing for me. passport.tsx
import { Strategy as MicrosoftStrategy } from "passport-microsoft";

passport.serializeUser(function(user: any, done) {
done(null, user._id);
});

passport.deserializeUser(async function(id: string, done) {

const user = await User.findById(id).lean().exec();
done(null, user);

});
import { Strategy as MicrosoftStrategy } from "passport-microsoft";

passport.serializeUser(function(user: any, done) {
done(null, user._id);
});

passport.deserializeUser(async function(id: string, done) {

const user = await User.findById(id).lean().exec();
done(null, user);

});
custom.d.ts
interface IUser {
_id: string;
username: string;
password?: string;
email: string;
firstName: string;
lastName?: string;
profilePicUrl?: string;
googleId?: string;
googleAccessToken?: string;
googleRefreshToken?: string;
microsoftId?: string
microsoftAccessToken?: string
microsoftRefreshToken?: string
meetings: string[];
bookings: string[];
}


declare global {
namespace Express {
interface Request {
user: IUser;
}
}
}
interface IUser {
_id: string;
username: string;
password?: string;
email: string;
firstName: string;
lastName?: string;
profilePicUrl?: string;
googleId?: string;
googleAccessToken?: string;
googleRefreshToken?: string;
microsoftId?: string
microsoftAccessToken?: string
microsoftRefreshToken?: string
meetings: string[];
bookings: string[];
}


declare global {
namespace Express {
interface Request {
user: IUser;
}
}
}
microsoftRoutes.tsx
router.get('/',
passport.authenticate('microsoft', {
}),
function(req, res) {
// The request will be redirected to Microsoft for authentication, so this
// function will not be called.
}
);

router.get('/callback',
passport.authenticate('microsoft', { failureRedirect: '/failure' }),
function(req, res) {
res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName}`);
}
);
router.get('/',
passport.authenticate('microsoft', {
}),
function(req, res) {
// The request will be redirected to Microsoft for authentication, so this
// function will not be called.
}
);

router.get('/callback',
passport.authenticate('microsoft', { failureRedirect: '/failure' }),
function(req, res) {
res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName}`);
}
);
req.user possibly 'undefined'
src/routes/microsoftRoutes.ts:30:70 - error TS18048: 'req.user' is possibly 'undefined'.
30 res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName`);

src/routes/microsoftRoutes.ts:30:79 - error TS2339: Property 'firstName' does not exist on type 'User'.

30 res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName`)
src/routes/microsoftRoutes.ts:30:70 - error TS18048: 'req.user' is possibly 'undefined'.
30 res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName`);

src/routes/microsoftRoutes.ts:30:79 - error TS2339: Property 'firstName' does not exist on type 'User'.

30 res.redirect(`http://localhost:5173/home/event-types?firstName=${req.user.firstName`)
req.user:Express.User but i want req.user:Express.User && IUser to fix the type issue
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server