first commit
This commit is contained in:
136
config/passport.js
Normal file
136
config/passport.js
Normal file
@@ -0,0 +1,136 @@
|
||||
import passport from "passport";
|
||||
import SteamStrategy from "passport-steam";
|
||||
import { config } from "./index.js";
|
||||
import User from "../models/User.js";
|
||||
|
||||
// Configure HTTP agent with timeout for Steam OpenID
|
||||
import https from "https";
|
||||
import http from "http";
|
||||
|
||||
const httpAgent = new http.Agent({
|
||||
timeout: 10000, // 10 second timeout
|
||||
keepAlive: true,
|
||||
});
|
||||
|
||||
const httpsAgent = new https.Agent({
|
||||
timeout: 10000, // 10 second timeout
|
||||
keepAlive: true,
|
||||
});
|
||||
|
||||
/**
|
||||
* Configure Passport with Steam Strategy
|
||||
*/
|
||||
export const configurePassport = () => {
|
||||
// Serialize user to session
|
||||
passport.serializeUser((user, done) => {
|
||||
done(null, user._id.toString());
|
||||
});
|
||||
|
||||
// Deserialize user from session
|
||||
passport.deserializeUser(async (id, done) => {
|
||||
try {
|
||||
const user = await User.findById(id);
|
||||
done(null, user);
|
||||
} catch (error) {
|
||||
done(error, null);
|
||||
}
|
||||
});
|
||||
|
||||
console.log("🔧 Configuring Steam Strategy...");
|
||||
console.log("Steam Realm:", config.steam.realm);
|
||||
console.log("Steam Return URL:", config.steam.returnURL);
|
||||
console.log(
|
||||
"Steam API Key:",
|
||||
config.steam.apiKey
|
||||
? "Set (length: " + config.steam.apiKey.length + ")"
|
||||
: "Not Set"
|
||||
);
|
||||
|
||||
// Configure Steam Strategy with options
|
||||
try {
|
||||
passport.use(
|
||||
new SteamStrategy(
|
||||
{
|
||||
returnURL: config.steam.returnURL,
|
||||
realm: config.steam.realm,
|
||||
apiKey: config.steam.apiKey,
|
||||
// Add HTTP agents for timeout control
|
||||
agent: httpAgent,
|
||||
profile: true,
|
||||
},
|
||||
async (identifier, profile, done) => {
|
||||
try {
|
||||
const steamId = profile.id;
|
||||
|
||||
// Find or create user
|
||||
let user = await User.findOne({ steamId });
|
||||
|
||||
if (user) {
|
||||
// Update existing user profile
|
||||
user.username = profile.displayName;
|
||||
user.avatar =
|
||||
profile.photos?.[2]?.value ||
|
||||
profile.photos?.[0]?.value ||
|
||||
null;
|
||||
user.communityvisibilitystate =
|
||||
profile._json?.communityvisibilitystate || 1;
|
||||
|
||||
await user.save();
|
||||
|
||||
console.log(
|
||||
`✅ Existing user logged in: ${user.username} (${steamId})`
|
||||
);
|
||||
} else {
|
||||
// Create new user
|
||||
user = new User({
|
||||
username: profile.displayName,
|
||||
steamId: steamId,
|
||||
avatar:
|
||||
profile.photos?.[2]?.value ||
|
||||
profile.photos?.[0]?.value ||
|
||||
null,
|
||||
account_creation:
|
||||
profile._json?.timecreated || Math.floor(Date.now() / 1000),
|
||||
communityvisibilitystate:
|
||||
profile._json?.communityvisibilitystate || 1,
|
||||
balance: 0,
|
||||
staffLevel: 0,
|
||||
});
|
||||
|
||||
await user.save();
|
||||
|
||||
console.log(
|
||||
`✅ New user registered: ${user.username} (${steamId})`
|
||||
);
|
||||
}
|
||||
|
||||
return done(null, user);
|
||||
} catch (error) {
|
||||
console.error("❌ Steam authentication error:", error);
|
||||
return done(error, null);
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
console.log("✅ Steam Strategy registered successfully");
|
||||
} catch (error) {
|
||||
console.error("❌ Failed to configure Steam Strategy:", error.message);
|
||||
console.error("This may be due to network issues or invalid configuration");
|
||||
}
|
||||
|
||||
console.log("🔐 Passport configured with Steam strategy");
|
||||
console.log(`📍 Steam Realm: ${config.steam.realm}`);
|
||||
console.log(`🔙 Return URL: ${config.steam.returnURL}`);
|
||||
console.log(`🔑 API Key: ${config.steam.apiKey ? "✅ Set" : "❌ Not Set"}`);
|
||||
|
||||
// Important note about Steam OpenID
|
||||
console.log("\n💡 Note: Steam OpenID discovery can sometimes fail due to:");
|
||||
console.log(" - Network/firewall blocking Steam's OpenID endpoint");
|
||||
console.log(" - Steam's service being temporarily unavailable");
|
||||
console.log(" - DNS resolution issues");
|
||||
console.log(
|
||||
" If /auth/steam fails, try: curl -v https://steamcommunity.com/openid"
|
||||
);
|
||||
};
|
||||
|
||||
export default configurePassport;
|
||||
Reference in New Issue
Block a user