Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the option to use a custom user manager to retrieve the user f… #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

tonibagur
Copy link

Adding the option to use a custom user manager to retrieve the user from cognito without having to create a custom user model
To use that feature, all you have to do is to configure the variable
COGNITO_USER_MANAGER=cognito_users.models.CognitoManager

Then you can define you own manager:
class CognitoManager(BaseUserManager):
def get_or_create_for_cognito(self, jwt_payload):
username = jwt_payload['cognito:username']
email = jwt_payload['email']
groups = jwt_payload['cognito:groups']
try:
user = get_user_model().objects.get(username=username)
except:
password = get_user_model().objects.make_random_password()
user = get_user_model().objects.create_user(username, email, password)
if 'superusers' in groups:
user.is_superuser = True
user.is_active = True
user.is_staff = True
user.save()
return user

In that way you don't need to customize your COGNITO_USER_MODEL which could be hard on an already running project.

Please, tell me if you would be interested in accepting a feature like that. If you are interested I can add documentation for it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant