I am trying to save the access as well as refresh tokens in my db to then make API calls for that user. I have followed the examples online as best I can for both Passport-js-spotify, JMPerez Passport Spotify example, and Spotify API documentation, but I still receive an error saying User.save() is not a function. I am able to retrieve the user information from the DB if I just findOne. Furthermore, I have a feeling that I am setting my callback up incorrectly as well. A little turned around with it all. I would greatly appreciate any help in resolving. I have included my code below. Please let me know if you need any additional information.
const express = require("express");
const router = express.Router();
const SpotifyStrategy = require("passport-spotify").Strategy;
const passport = require("passport");
const SpotifyUser = require("../Models/UserSpotify");
var spotifyID;
passport.use(
new SpotifyStrategy({
clientID: "84f1c0xxxx29b95xxx0b0fdxxx42f5ce",
clientSecret: "0b0xxx5944ad8bxx07ab4xxxx",
callbackURL: "http://localhost:3001/spotify/callback",
}, function (accessToken, refreshToken, expires_in, profile, done) {
spotifyID = profile.id
displayName = profile.displayName
email = profile._json.email
profileURL = profile.profileUrl
accessToken = accessToken
refreshToken = refreshToken
country = profile.country
accountType = profile.product
console.log(spotifyID)
process.nextTick(function () {
SpotifyUser.findOne({
'spotifyID': spotifyID
}).then((currentUser) => {
if (!currentUser) {
const User = new SpotifyUser == ({
spotifyID: spotifyID,
displayName: displayName,
email: email,
profileURL: profileURL,
accessToken: accessToken,
refreshToken: refreshToken,
country: country,
accountType: accountType
})
User.save(function (err) {
if (err) console.log(err);
return done(err, user);
})
} else {
console.log(currentUser);
}
})
})
router.get(
"/login",
passport.authenticate(
"spotify", {
scope: ["user-read-email", "user-read-private"],
showDialog: true,
}
),
function (req, res) {
console.log(res.accessToken)
console.log(res.refreshToken)
}
);
router.get(
"/callback",
passport.authenticate("spotify", {
failureRedirect: "/login"
}),
function (req, res) {
// Successful authentication, redirect home.
res.redirect("/success");
}
);
router.get("/success", passport.authenticate('spotify'), (req, res) => {
res.send("success")
});
One obvious thing I see in code is
const User = new SpotifyUser == ({
spotifyID: spotifyID,
displayName: displayName,
email: email,
profileURL: profileURL,
accessToken: accessToken,
refreshToken: refreshToken,
country: country,
accountType: accountType
})
Remove the ==
const User = new SpotifyUser({
spotifyID: spotifyID,
displayName: displayName,
email: email,
profileURL: profileURL,
accessToken: accessToken,
refreshToken: refreshToken,
country: country,
accountType: accountType
})