mirror of
https://github.com/RoboSats/robosats.git
synced 2024-12-13 02:46:28 +00:00
Handle auth and welcome back for existing users
This commit is contained in:
parent
80e0ca46fb
commit
d037506138
36
api/views.py
36
api/views.py
@ -14,6 +14,8 @@ from math import log2
|
||||
import numpy as np
|
||||
import hashlib
|
||||
from pathlib import Path
|
||||
from datetime import timedelta
|
||||
from django.utils import timezone
|
||||
|
||||
# Create your views here.
|
||||
|
||||
@ -143,25 +145,33 @@ class UserGenerator(APIView):
|
||||
# Create new credentials if nickname is new
|
||||
if len(User.objects.filter(username=nickname)) == 0:
|
||||
User.objects.create_user(username=nickname, password=token, is_staff=False)
|
||||
else:
|
||||
## TODO only report a match was found if it has
|
||||
## been at least 30 minutes since user creation
|
||||
## Why: frontend gets confused to say Welcome back too soon
|
||||
context['found'] = 'A matching nickname was found'
|
||||
|
||||
# TODO, "A matching nickname was found, but it is not yours!"
|
||||
# why? It is unlikely but there is only 20 billion names
|
||||
# but if the token is not exact
|
||||
|
||||
user = authenticate(request, username=nickname, password=token)
|
||||
if user is not None:
|
||||
user = authenticate(request, username=nickname, password=token)
|
||||
login(request, user)
|
||||
return Response(context, status=status.HTTP_201_CREATED)
|
||||
|
||||
return Response(context, status=status.HTTP_201_CREATED)
|
||||
else:
|
||||
user = authenticate(request, username=nickname, password=token)
|
||||
if user is not None:
|
||||
login(request, user)
|
||||
# Sends the welcome back message, only if created +30 mins ago
|
||||
if request.user.date_joined < (timezone.now()-timedelta(minutes=1)):
|
||||
context['found'] = 'We found your Robosat. Welcome back!'
|
||||
return Response(context, status=status.HTTP_202_ACCEPTED)
|
||||
else:
|
||||
# It is unlikely (1/20 Billions) but maybe the nickname is taken
|
||||
context['found'] = 'Bad luck, this nickname is taken'
|
||||
context['bad_request'] = 'Enter a different token'
|
||||
return Response(context, status=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
|
||||
|
||||
def delete(self,request):
|
||||
user = User.objects.get(id = request.user.id)
|
||||
|
||||
# TO DO. Pressing give me another will delete the logged in user
|
||||
# However it might be a long time recovered user
|
||||
# Only delete if user live is < 5 minutes
|
||||
|
||||
if user is not None:
|
||||
logout(request)
|
||||
user.delete()
|
||||
|
@ -120,7 +120,7 @@ export default class UserGenPage extends Component {
|
||||
this.state.found ?
|
||||
<Grid item xs={12} align="center">
|
||||
<Typography component="subtitle2" variant="subtitle2" color='primary'>
|
||||
We found your robosat, welcome back!<br/>
|
||||
{this.state.found}<br/>
|
||||
</Typography>
|
||||
<Button variant='contained' color='primary' to='/home' component={Link}>Cool!</Button>
|
||||
</Grid>
|
||||
|
Loading…
Reference in New Issue
Block a user