I get this error in my login file, when I test it with postman. I had to modify several things for typescript. Without typescript this file always has worked. So I could imagine, that it depends on the typescript elements or on the userinterface, where I have set “_doc”, because otherwise I get an error, that typescript not know what that is. So I put both snippets in this question. Thanks for your help.
My login:
authRouter.post('/login', async (request:Request, response:Response)=>{
let sec:string = process.env.JWT_SEC as string;
try{
const user = await User.findOne({username:request.body.username});
!user && response.status(401).json("Wrong credentials");
const hashedPassword = CryptoJS.AES.decrypt(user?.password, process.env.PASS_SEC);
const originalPassword = hashedPassword.toString(CryptoJS.enc.UTF8);
const inputPassword = request.body.password;
originalPassword !== inputPassword && response.status(401).json("Wrong Password")
const accessToken = jwt.sign(
{id: user!._id,
isAdmin:user!.isAdmin,
},
sec,
{expiresIn:"30d"}
)
const {password, ...others} = user?._doc;
response.status(200).json({...others, accessToken});
} catch(error:any){
response.status(401)
throw new Error(error)
}
});
My mongoose.model:
export interface UserDocument extends mongoose.Document{
vorname:string;
nachname:string;
username:string;
email:string;
street:string;
number:string;
plz:number;
city:string;
password:string;
isAdmin:boolean;
createdAt: Date;
updatedAt: Date;
_doc?: any;
organization: Types.ObjectId;
}
const UserSchema = new mongoose.Schema<UserDocument>({
vorname:{type:String, required:true},
nachname:{type:String, required:true},
username:{type:String, required:true },
email:{type:String, required:true },
street:{type:String, required:true },
number:{type:String, required:true },
plz:{type:Number, required:true },
city:{type:String, required:true },
password:{type:String, required:true },
isAdmin:{type:Boolean, default:false},
organization: { type: mongoose.Schema.Types.ObjectId, ref: 'Organization' }
},
{timestamps:true}
)
const User = mongoose.model<UserDocument>('User', UserSchema);