-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
getting an error "ReferenceError: User is not defined",how to fix it #7273
Comments
@mushthaque7 Thanks for posting! We'll take a look as soon as possible. In the mean time, there are a few ways you can help speed things along:
Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly. For help with questions about Sails, click here. |
@mushthaque7 This doesn't seem to use Sails. |
Hey @mushthaque7 as @eashaw this doesn't look like Sails hence I will be closing this issue. :) |
Node version: 18.14.2
Sails version (sails):
ORM hook version (sails-hook-orm):
Sockets hook version (sails-hook-sockets):
Organics hook version (sails-hook-organics):
Grunt hook version (sails-hook-grunt):
Uploads hook version (sails-hook-uploads):
DB adapter & version (e.g. sails-mysql@5.55.5):
Skipper adapter & version (e.g. skipper-s3@5.55.5):
//jshint esversion:6 require("dotenv").config(); const express = require("express"); //hash encryption method //const md5 = require("md5"); const bodyParser = require("body-parser"); const ejs = require("ejs"); // const bcrypt = require("bcrypt"); const mongoose = require("mongoose"); const session = require('express-session'); const passport = require("passport"); const findOrCreate = require('mongoose-find-or-create') const passportlocalMongoose = require("passport-local-mongoose"); var GoogleStrategy = require('passport-google-oauth20').Strategy; // const saltRounds=10; //const encrypt = require("mongoose-encryption") const app = express(); app.use(express.static("public")); app.use(bodyParser.urlencoded({extended:true})); app.set("view engine","ejs"); app.use(session({ secret: 'A little secret', resave: false, saveUninitialized: false })); app.use(passport.initialize()); mongoose.connect("mongodb://127.0.0.1:27017/userDB",{useNewUrlParser:true}); const userSchema =new mongoose.Schema({ email : String, password : String }); userSchema.plugin(passportlocalMongoose); userSchema.plugin(findOrCreate); //password encryption using mongoose-encryption //userSchema.plugin(encrypt,{secret:process.env.SECRET,encryptedFields: ['password']}); const user = mongoose.model("user",userSchema);
passport.use(user.createStrategy())
passport.serializeUser(function(user, cb) {
process.nextTick(function() {
cb(null, { id: user.id, username: user.username });
});
});
passport.deserializeUser(function(user, cb) {
process.nextTick(function() {
return cb(null, user);
});
})
passport.use(new GoogleStrategy({
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/secret",
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo"
},
function(accessToken, refreshToken, profile, cb) {
console.log(profile);
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return cb(err, user);
});
}
));
app.get("/",(req,res)=>{
res.render("home");
});
app.get('/auth/google',
passport.authenticate('google', { scope: ["profile"] }));
app.get("/register",(req,res)=>{
res.render("register");
});
app.get("/auth/google/secret",
passport.authenticate('google', { failureRedirect: '/login' }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect('/');
});
app.get("/login",(req,res)=>{
res.render("login");
});
app.get("/secrets",(req,res)=>{
if(req.isAuthenticated()){
res.render("secrets");
}else{
res.redirect("/login");
}
});
app.get("/logout",(req,res)=>{
req.logout(function(err) {
if (err) {
return next(err);
}
res.redirect('/');
});
})
app.post("/register", (req,res)=>{
user.register({username:req.body.username},req.body.password,function(err,user){
if(err){
console.log(err)
res.redirect("/register");
}else{
passport.authenticate('local', {successRedirect: '/secrets',failureRedirect: '/register'});
}
});
});
app.post("/login",(req,res)=>{
const users = new user({
username : req.body.username,
password : req.body.password
});
req.login(users,function(err){
if(err){
console.log(err);
}else{
passport.authenticate("local")(req,res,function(){
res.redirect("/secrets")
});
}
})
});
app.listen(3000,function(){
console.log("Server started");
});
error output
ReferenceError: User is not defined
at Strategy._verify (C:\Users\MUSHTHAQUE\Downloads\Authentication\app.js:59:5)
at C:\Users\MUSHTHAQUE\Downloads\Authentication\node_modules\passport-oauth2\lib\strategy.js:205:24
at C:\Users\MUSHTHAQUE\Downloads\Authentication\node_modules\passport-google-oauth20\lib\strategy.js:122:5
at passBackControl (C:\Users\MUSHTHAQUE\Downloads\Authentication\node_modules\oauth\lib\oauth2.js:134:9)
at IncomingMessage. (C:\Users\MUSHTHAQUE\Downloads\Authentication\node_modules\oauth\lib\oauth2.js:157:7)
at IncomingMessage.emit (node:events:525:35)
at endReadableNT (node:internal/streams/readable:1359:12)
at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
The text was updated successfully, but these errors were encountered: