Last active
December 11, 2019 12:29
-
-
Save carlosalvarez91/138ae78f31314b4d14f0bf6b07faf662 to your computer and use it in GitHub Desktop.
Verify email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { graphql, compose, withApollo } from 'react-apollo' | |
import openNotification from '../components/NotificationComponent' | |
import gql from 'graphql-tag'; | |
const mutations = { | |
verifyToken: gql` | |
mutation VerifyToken($token: String!) { | |
verifyToken(token: $token) { | |
user{ | |
isVerified | |
} | |
} | |
} | |
`} | |
class SomeComponent extends React.Component { | |
componentDidMount(){ | |
let params = this.props.location.search | |
if(params){ | |
let token = params.replace('?token=', '') | |
console.log(token) | |
//mutation set user is_verified, passing the token of the url. | |
this.props | |
.verifyToken({ variables: { token: token } }) | |
.then(res => { | |
console.log(res) | |
openNotification('Your email is verified','You can now log in') | |
}) | |
.catch(err => { | |
console.log('Unvalid token', err) | |
openNotification('Error','Unvalid token') | |
}) | |
} | |
} | |
render() { | |
return ( | |
<div> | |
text | |
</div> | |
); | |
} | |
} | |
SomeComponent = compose( | |
withApollo, | |
graphql(mutations.verifyToken, {name:"verifyToken"}) | |
)(LoginForm) | |
export default SomeComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from graphql_jwt.utils import jwt_encode, jwt_payload, jwt_decode | |
from django.contrib.auth import get_user_model | |
import graphene | |
from graphql import GraphQLError | |
class UserSchema(DjangoObjectType): | |
class Meta: | |
model = get_user_model() | |
class VerifyToken(graphene.Mutation): | |
user =graphene.Field(UserSchema) | |
class Arguments: | |
token = graphene.String(required = True) | |
def mutate(self, info, token): | |
payload = jwt_decode(token, context=None) | |
try: | |
#this should get the user by email instead of username | |
user = get_user_model().objects.get(username=payload['username']) | |
user.is_verified = True | |
user.save() | |
except: | |
raise GraphQLError('No User with this email!') | |
return VerifyToken(user=user) | |
class Mutation(graphene.ObjectType): | |
verify_token = VerifyToken.Field()#graphql_jwt.Verify.Field() | |
''' | |
mutation VerifyToken($token: String!) { | |
verifyToken(token: $token) { | |
user{ | |
isVerified | |
} | |
} | |
} | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment