0
votes

i was doing OAuth2 login for my website. When user logged on my OAuth2 login, bot must give role to logged user (in my Server) But when i logged in, bot gives error (sorry for my english)

Thank you all for helps from now

There is my code:

const Discord = require('discord.js');
const client = new Discord.Client();
const app = express();
const passport = require("passport");
const { Strategy } = require("passport-discord");
const session = require("express-session");

app.use(passport.initialize());
app.use(passport.session());

passport.serializeUser((user, done) => {
    client.guilds.cache.get("843133197830324265").members.cache.get(user.id).roles.add("844616320154861588")
    return done(null, user)
  });

passport.deserializeUser((obj, done) => done(null, obj));

const scopes = ["identify", "guilds"];
passport.use(new Strategy({
      clientID: "clientid",
      clientSecret: "clientsecret",
      callbackURL: "http://localhost:3000/callback",
      scope: [ "guilds", "identify" ],
    },
    (accessToken, refreshToken, profile, done) => {
      process.nextTick(() => done(null, profile));
    })
);

app.get("/login", passport.authenticate("discord", { scope: ["identify", "guilds" ], }));
app.get("/callback", passport.authenticate("discord", { failureRedirect: "/error", }), (req, res) => res.redirect("/"));
app.get("/logout", (req, res) => {
  req.logOut();
  return res.redirect("/");
});
const listener = app.listen(3000, err => {
    if (err) throw err;
        console.log(`Site ${listener.address().port} portunda hazır!`);

})

client.login("token"); ```


And the error : 

```TypeError: Cannot read property 'roles' of undefined
    at C:\Users\ulaum\OneDrive\Masaüstü\oauth2\index.js:13:77
    at pass (C:\Users\ulaum\OneDrive\Masaüstü\oauth2\node_modules\passport\lib\authenticator.js:294:9)
    at Authenticator.serializeUser (C:\Users\ulaum\OneDrive\Masaüstü\oauth2\node_modules\passport\lib\authenticator.js:299:5)
    at SessionManager.logIn (C:\Users\ulaum\OneDrive\Masaüstü\oauth2\node_modules\passport\lib\sessionmanager.js:14:8)
    at IncomingMessage.req.login.req.logIn (C:\Users\ulaum\OneDrive\Masaüstü\oauth2\node_modules\passport\lib\http\request.js:50:33)     
    at Strategy.strategy.success (C:\Users\ulaum\OneDrive\Masaüstü\oauth2\node_modules\passport\lib\middleware\authenticate.js:253:13)   
    at verified (C:\Users\ulaum\OneDrive\Masaüstü\oauth2\node_modules\passport-oauth2\lib\strategy.js:186:20)
    at C:\Users\ulaum\OneDrive\Masaüstü\oauth2\index.js:28:30
    at processTicksAndRejections (node:internal/process/task_queues:76:11)```
1

1 Answers

0
votes
passport.serializeUser((user, done) => {
    client.guilds.cache.get("843133197830324265").members.cache.get(user.id).roles.add("844616320154861588")
    return done(null, user)
  });

Here, the add method requires a Discord.Role argument, but you are feeding it a string argument (the role's ID). You have to fetch the role object that corresponds to that ID before trying to add it to the user.

Try something like this:

passport.serializeUser((user, done) => {
    const newRole = client.guilds.cache.get("843133197830324265").roles.cache.get("844616320154861588")
    client.guilds.cache.get("843133197830324265").members.cache.get(user.id).roles.add(newRole)
    return done(null, user)
  });