Skip to content

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