-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-> Individual login providers are moved into respective folders -> Fix problem of not being able to use individual login methods -> Now it's relatively easier to use the library
- Loading branch information
Showing
11 changed files
with
259 additions
and
252 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from kivy.logger import Logger | ||
from jnius import autoclass, PythonJavaClass, java_method | ||
from android.activity import bind as result_bind | ||
|
||
FirebaseAuth = autoclass("com.google.firebase.auth.FirebaseAuth") | ||
OAuthProvider = autoclass("com.google.firebase.auth.OAuthProvider") | ||
|
||
PythonActivity = autoclass("org.kivy.android.PythonActivity") | ||
|
||
context = PythonActivity.mActivity | ||
|
||
event_success_listener = None | ||
event_error_listener = None | ||
|
||
|
||
class OnSuccessListener(PythonJavaClass): | ||
__javainterfaces__ = ["com/google/android/gms/tasks/OnSuccessListener"] | ||
__javacontext__ = "app" | ||
|
||
def __init__(self, success_listener): | ||
self.success_listener = success_listener | ||
|
||
@java_method("(Ljava/lang/Object;)V") | ||
def onSuccess(self, result): | ||
# user is signed in | ||
Logger.info("KivyAuth: Sign in successful using firebase.") | ||
user = FirebaseAuth.getInstance().getCurrentUser() | ||
|
||
self.success_listener( | ||
user.getDisplayName(), user.getEmail(), user.getPhotoUrl().toString() | ||
) | ||
|
||
|
||
class OnFailureListener(PythonJavaClass): | ||
__javainterfaces__ = ["com/google/android/gms/tasks/OnFailureListener"] | ||
__javacontext__ = "app" | ||
|
||
def __init__(self, error_listener): | ||
self.error_listener = error_listener | ||
|
||
@java_method("(Ljava/lang/Exception;)V") | ||
def onFailure(self, e): | ||
# handle exception | ||
Logger.info("KivyAuth: Sign in using firebase failed") | ||
self.error_listener() | ||
|
||
|
||
def initialize_firebase(success_listener, error_listener): | ||
FirebaseAuth.getInstance() | ||
Logger.info("KivyAuth: Initialized firebase auth") | ||
global event_success_listener | ||
event_success_listener = success_listener | ||
global event_error_listener | ||
event_error_listener = error_listener | ||
|
||
|
||
def firebase_login(provider): | ||
pendingResultTask = FirebaseAuth.getPendingAuthResult() | ||
if pendingResultTask: | ||
# There's something already here! Finish the sign-in for your user. | ||
|
||
task = pendingResultTask.addOnSuccessListener( | ||
OnSuccessListener(event_success_listener) | ||
) | ||
task = task.addOnFailureListener(OnFailureListener(event_error_listener)) | ||
else: | ||
# There's no pending result so you need to start the sign-in flow. | ||
|
||
task = FirebaseAuth.startActivityForSignInWithProvider( | ||
context, provider.build() | ||
) | ||
task = task.addOnSuccessListener(OnSuccessListener(event_success_listener)) | ||
task = task.addOnFailureListener(OnFailureListener(event_error_listener)) | ||
|
||
|
||
def firebase_logout(after_logout): | ||
Logger.info("KivyAuth: Initiated logout using firebase") | ||
FirebaseAuth.getInstance().signOut() | ||
after_logout() | ||
Logger.info("KivyAuth: Logged out from firebase auth") | ||
|
||
|
||
def login_github(): | ||
Logger.info("KivyAuth: Initiated github login") | ||
provider = OAuthProvider.newBuilder("github.com") | ||
firebase_login(provider) | ||
|
||
|
||
def login_twitter(): | ||
Logger.info("KivyAuth: Initiated twitter login") | ||
provider = OAuthProvider.newBuilder("twitter.com") | ||
firebase_login(provider) | ||
|
||
|
||
def logout_twitter(after_logout): | ||
firebase_logout(after_logout) | ||
|
||
|
||
def logout_github(after_logout): | ||
firebase_logout(after_logout) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from kivy.logger import Logger | ||
from kivy.clock import mainthread | ||
from jnius import autoclass | ||
from android.activity import bind as result_bind | ||
|
||
Gso = autoclass("com.google.android.gms.auth.api.signin.GoogleSignInOptions") | ||
GsoBuilder = autoclass( | ||
"com.google.android.gms.auth.api.signin.GoogleSignInOptions$Builder" | ||
) | ||
GSignIn = autoclass("com.google.android.gms.auth.api.signin.GoogleSignIn") | ||
ApiException = autoclass("com.google.android.gms.common.api.ApiException") | ||
PythonActivity = autoclass("org.kivy.android.PythonActivity") | ||
context = PythonActivity.mActivity | ||
|
||
RC_SIGN_IN = 10122 | ||
mGSignInClient = None | ||
|
||
|
||
class GoogleActivityListener: | ||
def __init__(self, success_listener, error_listener): | ||
self.success_listener = success_listener | ||
self.error_listener = error_listener | ||
|
||
def google_activity_listener(self, request_code, result_code, data): | ||
if request_code == RC_SIGN_IN: | ||
Logger.info("KivyAuth: google_activity_listener called.") | ||
task = GSignIn.getSignedInAccountFromIntent(data) | ||
try: | ||
account = task.getResult(ApiException) | ||
if account: | ||
Logger.info( | ||
"KivyAuth: Google Login success.\ | ||
Calling success listener." | ||
) | ||
self.success_listener( | ||
account.getDisplayName(), | ||
account.getEmail(), | ||
account.getPhotoUrl().toString(), | ||
) | ||
|
||
except Exception as e: | ||
Logger.info("KivyAuth: Error signing in using Google. {}".format(e)) | ||
self.error_listener() | ||
|
||
|
||
def initialize_google(success_listener, error_listener): | ||
gso = GsoBuilder(Gso.DEFAULT_SIGN_IN).requestEmail().build() | ||
global mGSignInClient | ||
mGSignInClient = GSignIn.getClient(context, gso) | ||
gal = GoogleActivityListener(success_listener, error_listener) | ||
result_bind(on_activity_result=gal.google_activity_listener) | ||
|
||
Logger.info("KivyAuth: Initialized google signin") | ||
|
||
|
||
# @mainthread | ||
def login_google(): | ||
Logger.info("KivyAuth: Initiated google login") | ||
signInIntent = mGSignInClient.getSignInIntent() | ||
context.startActivityForResult(signInIntent, RC_SIGN_IN) | ||
|
||
|
||
def logout_google(after_logout): | ||
mGSignInClient.signOut() | ||
after_logout() | ||
Logger.info("KivyAuth: Logged out from google login") |
Oops, something went wrong.