سؤال

Im using a custom User model for registering users in my site. The way it works is, when a user registers, he/she starts out with 0 reputation, similar to Stack overflow. That is, a new instance of User model is created for that user.

Now I would like to add OAuth to my website but, I dont understand how when a new user logs into my website with OAuth will a new django user model instance get created for that user, such that it is same like registering with my website and having the user start with reputation 0.

Just like how stack overflow implements, if a user dosent have a SO account, he can still login with a google account and have his stack overflow profile get created as though he registered with SO.

I hope my question is clear, I just need to know how things work together in this scenario. Thanks y'all.

هل كانت مفيدة؟

المحلول

OAuth is only a login flow where the authentication and data is supplied by a third party. E.g. Google, Facebook, Twitter, OpenID, Yahoo, etc. etc. The flow is basically like this:

  1. User visits your site and clicks a "Login with XYZ" button.
  2. YOU redirect the user to a third party OAuth point. E.g. https://www.facebook.com/login.php
  3. User logs in with the third party
  4. THIRD PARTY redirects the user back to your domain with some sort of code/token www.example.com/signin/?code=abcdef
  5. YOU exchange the code/token with the THIRD PARTY for user information e.g. name/email. You then either:
    • YOU create an account on your system with this information.
    • YOU find an existing account for this user and log the user in as that user.

This is a bit abstract and only part of all the options of OAuth but it covers the basic login flow. All the blocks with YOU in there are steps that you need to prepare/code. E.g. a view which takes a code/token and exchanges that for data with which you can login/create an account for the user.

Most third parties have pretty good documentation on how to accomplish the signup using their APIs.

But you will have to create quite some logic manually as such there is no benefit or drawback to using a custom user model.

If this doesn't answer your question please elaborate a bit on what you meant.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top