Last active
August 31, 2019 09:45
-
-
Save gagamil/af8b3321bb23cbb589f866978221fd3d to your computer and use it in GitHub Desktop.
Code for blog post https://www.gabrielgamil.com/posts/firebase-custom-auth-sys-django/
This file contains hidden or 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
firebase.auth().onAuthStateChanged(function(user) { | |
if (user) { | |
console.log("User authenticated"); | |
} else { | |
console.log("User lost"); | |
} | |
}); |
This file contains hidden or 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
class AuthenticateView(APIView): | |
def post(self, request, format=None): | |
serializer = AuthenticationSerializer(data=request.data) | |
if serializer.is_valid(): | |
username = serializer.validated_data['username'] | |
password = serializer.validated_data['password'] | |
user = authenticate(request, username=username, password=password) | |
if user is not None: | |
additional_claims = { | |
'tenant': str(user.userprofile.tenant.id) | |
} | |
custom_token = auth.create_custom_token( | |
str(user.id), additional_claims) | |
ts = TokenSerializer({'token': custom_token.decode('UTF-8')}) | |
return Response(ts.data, status=status.HTTP_200_OK) | |
return Response(status=status.HTTP_406_NOT_ACCEPTABLE) |
This file contains hidden or 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
const useFetchJWTFromDjangoApp = credentials => { | |
const [authResponseData, setAuthResponseData] = useState({ | |
JWT: "", | |
error: false | |
}); | |
useEffect(() => { | |
if (credentials) { | |
fetch("/api/authenticate/", { | |
method: "post", | |
body: JSON.stringify(credentials), | |
headers: { | |
"Content-Type": "application/json" | |
} | |
}) | |
.then(response => { | |
if (response.ok) { | |
return response.json(); | |
} | |
throw new Error("Network response bad."); | |
}) | |
.then(data => { | |
setAuthResponseData({ JWT: data.token, error: false }); | |
}) | |
.catch(error => { | |
return setAuthResponseData({ JWT: "", error: true }); | |
}); | |
} | |
}, [credentials]); | |
return authResponseData; | |
}; |
This file contains hidden or 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
const useSignInWithCustomToken = (JWT, firebase) => { | |
useEffect(() => { | |
if (JWT && firebase) { | |
firebase | |
.signInWithToken(JWT) | |
.then(() => { | |
const base64Url = JWT.split(".")[1]; | |
const decodedValue = JSON.parse(atob(base64Url)); | |
localStorage.setItem("tenantId", decodedValue.claims.tenant); | |
}) | |
.catch(function(error) { | |
console.error("Error logging in: ", error); | |
}); | |
} | |
}, [JWT, firebase]); | |
}; |
This file contains hidden or 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
class UserProfile(models.Model): | |
user = models.OneToOneField(User, on_delete='CASCADE') | |
tenant = models.ForeignKey(Tenant, on_delete='CASCADE') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment