OAuth not requiring new login when new people come to the site

We are using OAuth to be able to sign in with Battle.Net. However, right now … Read more OAuth not requiring new login when new people come to the site

We are using OAuth to be able to sign in with Battle.Net. However, right now if I sign in to my battle.net and then someone else comes to the site they will be logged into my battle.net.

We are thinking that the issue resides in our server file where when someone else comes to the site the user variable is still filled with whoever logged in last. We cant for the life of us figure out how to fix this. Does anyone have experience with this?

let user = {};

let app = express();

dotenv.config();
const BNET_ID = process.env.BNET_ID;
const BNET_SECRET = process.env.BNET_SECRET;

passport.serializeUser((user, cb) => {
    cb(null, user);
});

passport.deserializeUser((user, cb) => {
    cb(null, user);
});

// Use the BnetStrategy within Passport.
passport.use(
    new BnetStrategy(
        {
            clientID: BNET_ID,
            clientSecret: BNET_SECRET,
            callbackURL: "http://localhost:5000/auth/bnet/callback",
            region: "us",
            scope: "openid",
        },
        function (accessToken, refreshToken, profile, done) {
            user = profile;
            return done(null, profile);
        },
    ),
);

app.use(cors());
app.use(express.static(path.join(__dirname, "client", "build")))
app.options("*", cors());
app.use(express.json());
app.use(passport.initialize());

app.get("/auth/bnet", passport.authenticate("bnet"));

app.get(
    "/auth/bnet/callback",
    passport.authenticate("bnet", { failureRedirect: "/" }),
    function (req, res) {
        res.redirect("http://localhost:3000/");
    },
);

app.get("/user", (req, res) => {
    res.send(user);
});

app.get("/auth/logout", (req, res) => {
    console.log("logging out!");
    user = {};
    res.redirect("http://localhost:3000/");
});

app.use("/api/v1/brackets", brackets);
app.use("/api/v1/userBrackets", userBrackets);
app.get("*", (req, res) => {res.sendFile(path.join(__dirname, "client", "build", "index.html"));});
app.use("*", (req, res) => res.status(404).json({ error: "not found" }));

export default app;

Source: React – Stack Overflow



Leave a Reply

Your email address will not be published. Required fields are marked *