Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions config/auth.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module.exports = {
ensureAuthenticated: function(req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
},
forwardAuthenticated: function(req, res, next) {
if (!req.isAuthenticated()) {
return next();
}
res.redirect('/dashboard');
ensureAuthenticated: function (req, res, next) {
if (req.isAuthenticated()) {
return next();
}
};
res.redirect("/login");
},
forwardAuthenticated: function (req, res, next) {
if (!req.isAuthenticated()) {
return next();
}
res.redirect("/dashboard");
},
};
26 changes: 13 additions & 13 deletions config/passport.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcryptjs');
const LocalStrategy = require("passport-local").Strategy;
const bcrypt = require("bcryptjs");

const User = require('../models/userSchema');
const User = require("../models/userSchema");

module.exports = function(passport) {
module.exports = function (passport) {
passport.use(
new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
new LocalStrategy({ usernameField: "email" }, (email, password, done) => {
User.findOne({
email: email
}).then(user => {
email: email,
}).then((user) => {
if (!user) {
return done(null, false, { message: 'That email is not registered' });
return done(null, false, { message: "That email is not registered" });
}

bcrypt.compare(password, user.password, (err, isMatch) => {
if (err) throw err;
if (isMatch) {
return done(null, user);
} else {
return done(null, false, { message: 'Password incorrect' });
return done(null, false, { message: "Password incorrect" });
}
});
});
})
);

passport.serializeUser(function(user, done) {
passport.serializeUser(function (user, done) {
done(null, user.id);
});

passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
passport.deserializeUser(function (id, done) {
User.findById(id, function (err, user) {
done(err, user);
});
});
};
};
99 changes: 54 additions & 45 deletions controllers/authController.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
const User = require("../models/userSchema")
const bcrypt = require("bcryptjs")
const User = require("../models/userSchema");
const bcrypt = require("bcryptjs");

const register = async(req, res)=>{
let errors = [];
const {username, email, password, modelNumber, emergencyContact} = req.body
if(!username || !email || !password){
errors.push({msg:"Please Fill in all the fields"})
}
const register = async (req, res) => {
const errors = [];
const { username, email, password, modelNumber, emergencyContact } = req.body;
if (!username || !email || !password) {
errors.push({ msg: "Please Fill in all the fields" });
}

if(username.length < 5 || username.length > 17 ){
errors.push({msg:"username should be in between 6 and 16 characters"})
}
if (username.length < 5 || username.length > 17) {
errors.push({ msg: "username should be in between 6 and 16 characters" });
}

if(password.length < 5 || password.length > 17 ){
errors.push({msg: "Password should be in between 6 and 16 characters"})
}
if (password.length < 5 || password.length > 17) {
errors.push({ msg: "Password should be in between 6 and 16 characters" });
}

if(errors.length > 0){
res.render("register", {errors, username, email, password, modelNumber, emergencyContact})
}else{
User.findOne({email:email})
.then(user => {
if(user){
errors.push({msg:"User already exists, try logging in instead."})
res.render("register", {error})
}else{
const newUser = new User({
"username": username,
"email": email,
"password": password,
"modelNumber": modelNumber,
"emergencyContact": emergencyContact
})
bcrypt.genSalt(10, (err, salt)=> bcrypt.hash(newUser.password, salt, (err, hash)=>{
if(err) throw err
if (errors.length > 0) {
res.render("register", {
errors,
username,
email,
password,
modelNumber,
emergencyContact,
});
} else {
User.findOne({ email: email }).then((user) => {
if (user) {
errors.push({ msg: "User already exists, try logging in instead." });
res.render("register", { error });
} else {
const newUser = new User({
username: username,
email: email,
password: password,
modelNumber: modelNumber,
emergencyContact: emergencyContact,
});
bcrypt.genSalt(10, (err, salt) =>
bcrypt.hash(newUser.password, salt, (err, hash) => {
if (err) throw err;

newUser.password = hash;
newUser.password = hash;

newUser.save()
.then(user => {
res.redirect("/dashboard")
})
.catch(err=>console.log(err));
}))
}
})
}
}
newUser
.save()
.then((user) => {
res.redirect("/dashboard");
})
.catch((err) => );
})
);
}
});
}
};

module.exports = {
register
}
register,
};
70 changes: 31 additions & 39 deletions controllers/spotifyController.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
const spotifyApi = require("../routes/spotifyRoute")
var spotifyData = null;

const spotifyApi = require("../routes/spotifyRoute");
const spotifyData = null;

const spotifyAuth = async (req, res, next) => {
var code = req.query.code;
if (req.query.code !== null) {
var code = req.query.code;
if (req.query.code != null) {
var code = req.query.code;

await spotifyApi.spotifyApi.authorizationCodeGrant(code).then(
data=>{
const access_token = data.body['access_token'];
const refresh_token = data.body['refresh_token'];
spotifyApi.spotifyApi.setAccessToken(access_token);
spotifyApi.spotifyApi.setRefreshToken(refresh_token);
console.log('Spotify Authorized')
}
).catch(err => {
console.log(err);
})
// spotifyApi.spotifyApi.setAccessToken(code);
spotifyApi.spotifyApi.getMyCurrentPlaybackState().then(
function(data) {
if(data.body && data.body.is_playing){
console.log(data.body["item"]["name"])
res.render('index')
} else {
console.log('Nothing is Playing right now');
res.render('index')
}
}, function(err) {
console.log('Something Went Wrong', err)
}
)

}
else {
res.render("index")
}
}

await spotifyApi.spotifyApi
.authorizationCodeGrant(code)
.then((data) => {
const access_token = data.body.access_token;
const refresh_token = data.body.refresh_token;
spotifyApi.spotifyApi.setAccessToken(access_token);
spotifyApi.spotifyApi.setRefreshToken(refresh_token);
})
.catch((err) => {});
// spotifyApi.spotifyApi.setAccessToken(code);
spotifyApi.spotifyApi.getMyCurrentPlaybackState().then(
function (data) {
if (data.body && data.body.is_playing) {
res.render("index");
} else {
res.render("index");
}
},
function (err) {}
);
} else {
res.render("index");
}
};

module.exports = {
spotifyAuth,
}
spotifyAuth,
};
71 changes: 33 additions & 38 deletions controllers/weatherController.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
const https = require("https")
const https = require("https");


const getWeather = (req, res)=>{
const getWeather = (req, res) => {
return new Promise((resolve, reject) => {
https.get("https://api.myip.com/", function(response){
response.on("data", function(data){
const ipData = JSON.parse(data)
const ip = ipData.ip
https.get(`https://ipwhois.app/json/${ip}`, (ipResponse) => {
ipResponse.on("data", (locData) => {
const resp = JSON.parse(locData)
const location = resp.city
const query = location;
const apiKey = process.env.WEATHER_API_KEY
const units = "metric";
const url =
"https://api.openweathermap.org/data/2.5/weather?q=" +
query +
"&appid=" +
apiKey +
"&units=" +
units;

https.get(url, function (response) {
console.log(response.statusCode);

response.on("data", function (data) {
const weatherData = JSON.parse(data);
resolve(weatherData);
})
})
})
})
})
})
})
}

https.get("https://api.myip.com/", function (response) {
response.on("data", function (data) {
const ipData = JSON.parse(data);
const ip = ipData.ip;
https.get(`https://ipwhois.app/json/${ip}`, (ipResponse) => {
ipResponse.on("data", (locData) => {
const resp = JSON.parse(locData);
const location = resp.city;
const query = location;
const apiKey = process.env.WEATHER_API_KEY;
const units = "metric";
const url =
"https://api.openweathermap.org/data/2.5/weather?q=" +
query +
"&appid=" +
apiKey +
"&units=" +
units;

https.get(url, function (response) {
response.on("data", function (data) {
const weatherData = JSON.parse(data);
resolve(weatherData);
});
});
});
});
});
});
});
};

module.exports = { getWeather }
module.exports = { getWeather };
Loading