Giter Site home page Giter Site logo

Comments (9)

pgrodrigues avatar pgrodrigues commented on May 30, 2024 1

I guess you just need to:

  1. Create a user schema using mongoose, which will hold your users data. Be sure that schema has specific fields for username and other steam account data that can be useful in your app (such as profileUrl, profileImageUrl, steamId, etc).
  2. When the user logs in and given the fact the login was successful, just check if the user exists (for example, by searching for its steamId like:
passport.use(new SteamStrategy({
    returnURL: 'http://localhost:3000/auth/steam/return',
    realm: 'http://localhost:3000/',
    apiKey: 'your steam API key'
  }, function (identifier, profile, done) {
    var steamId = identifier.match(/\d+$/)[0];
    var profileURL = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' + 'your steam API key' + '&steamids=' + steamId;

    User.findOne({ steamId: steamId }, function (err, user) {
      if (user) {  // If the user already exists
          // Edit fields that might be outdated such as steam user image...
      }
      else { // If the user does not exist create a new one
           request(profileURL, function (error, response, body) {
                if (!error && response.statusCode === 200) {
                     var data = JSON.parse(body);
                     var profile = data.response.players[0];

                     var user = new User();
                     user.username = profile.personaname;
                     user.profileURL = profile.profileurl;
                     user.profileImageURL = profile.avatarmedium;
                     user.steamId = steamId;

                     user.save(function (err) {
                         done(err, user);
                     });
                } else {
                    done(error, null);
                }
           });
       }
    });

Of course this is just a starting point, you'll have to adjust this according to your needs.

from passport-steam.

andrewda avatar andrewda commented on May 30, 2024 1

Yep, @pgrodrigues just about nailed it. That's exactly what you'll need to get it working on a basic level. You might be able to lighten up the code a little by just using the update() method's upsert option, which will edit a document or create one if it does not exist. For example:

User.update({ steamid }, {
	$set: profile
}, {
	upsert: true
}, (err) => {
	if (err) {
		console.log(err);
	}
});

This will update a document with the steamid field equal to the steamid variable (using some ES6 magic on that first line... makes stuff look cleaner). Then we override the existing document with our updated profile, and, if the document does not exist, it will be created thanks to our upsert: true option.

from passport-steam.

pgrodrigues avatar pgrodrigues commented on May 30, 2024 1

You're right @andrewda. Either update() or findOneAndUpdate() with upsert: true is the way to go.

from passport-steam.

andrewda avatar andrewda commented on May 30, 2024

Correct me if I'm wrong, but passport-local-mongoose seems to be a way to easily store usernames and hashed passwords in your database. The purpose of using passport-steam is to not have to store passwords in your database – users login on the Steam servers instead.

It sounds like you might be looking for a session store, though that is not what passport-local-mongoose does. If that's the case, checkout the Express session store documentation and connect-mongo.

Tell me if none of this is what you're asking, but hope this helps!

from passport-steam.

Falden21 avatar Falden21 commented on May 30, 2024

I'm reading these, but to make it simple. I would like to store them as a user in the database once they've logged in with steam. This way other things can be associated with them like forum posts, comments, etc.

Sorry if I was being unclear

from passport-steam.

andrewda avatar andrewda commented on May 30, 2024

Ah okay, I understand. There is no module right now AFAIK that is taking on this task, but it's not difficult to accomplish by yourself. It shouldn't take more than ~20 lines to store all of a user's data in a MongoDB/Mongoose database.

from passport-steam.

Falden21 avatar Falden21 commented on May 30, 2024

Would you mind hinting me towards the right direction to accomplish this? It would be extremely helpful.

from passport-steam.

Falden21 avatar Falden21 commented on May 30, 2024

Thanks everyone, you have been a huge help!

from passport-steam.

pgrodrigues avatar pgrodrigues commented on May 30, 2024

No problem :) issue can be closed.

from passport-steam.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.