Skip to content
Snippets Groups Projects
Commit 7b56c012 authored by darshanxyz's avatar darshanxyz
Browse files

Added Google Login Functionality

parent a13c45a7
Branches
No related tags found
No related merge requests found
...@@ -25,6 +25,8 @@ dependencies { ...@@ -25,6 +25,8 @@ dependencies {
exclude group: 'com.android.support', module: 'support-annotations' exclude group: 'com.android.support', module: 'support-annotations'
}) })
compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.google.firebase:firebase-auth:10.2.1'
compile 'com.google.android.gms:play-services-auth:10.2.1'
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
} }
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".Registration"></activity> <activity android:name=".MainActivity"></activity>
</application> </application>
</manifest> </manifest>
\ No newline at end of file
package com.darshanbshah.odsystem; package com.darshanbshah.odsystem;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class Login extends AppCompatActivity { import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
public class Login extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener{
private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private static int RC_SIGN_IN = 0;
private static String TAG = "MAIN_ACTIVITY";
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login); setContentView(R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
Log.d("AUTH", "User logged in: " + user.getEmail());
}
else {
Log.d("AUTH", "User logged out.");
}
}
};
GoogleSignInOptions options = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestIdToken(getString(R.string.default_web_client_id)).requestEmail().build();
mGoogleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this, this).addApi(Auth.GOOGLE_SIGN_IN_API, options).build();
findViewById(R.id.sign_in_button).setOnClickListener(this);
}
public void login() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "Connection failed");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.sign_in_button:
login();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
FirebaseAuthGoogle(account);
}
else {
Log.d(TAG, "Google login failed");
}
}
}
public void FirebaseAuthGoogle(GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("AUTH", "signInWithCredential: onComplete: " + task.isSuccessful());
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
} }
} }
\ No newline at end of file
package com.darshanbshah.odsystem; package com.darshanbshah.odsystem;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.view.View;
public class Registration extends AppCompatActivity { import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration); setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
}
public void signOut(View view) {
mAuth.signOut();
startActivity(new Intent(this, Login.class));
finish();
} }
} }
...@@ -10,61 +10,24 @@ ...@@ -10,61 +10,24 @@
android:paddingTop="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.darshanbshah.odsystem.Login"> tools:context="com.darshanbshah.odsystem.Login">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="185dp"
android:id="@+id/emailField"
android:hint="Email"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textAlignment="center" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/passwordField"
android:hint="Password"
android:fontFamily="sans-serif"
android:layout_marginTop="43dp"
android:layout_below="@+id/emailField"
android:layout_alignStart="@+id/emailField"
android:textAlignment="center" />
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="41dp"
android:id="@+id/loginButton"
android:layout_below="@+id/passwordField"
android:layout_centerHorizontal="true"
style="@style/Widget.AppCompat.Button.Colored" />
<TextView <TextView
android:text="OD SYSTEM" android:text="OD SYSTEM"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="68dp" android:layout_marginTop="12dp"
android:id="@+id/textView3" android:id="@+id/textView3"
android:typeface="sans" android:typeface="sans"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:textAppearance="@style/TextAppearance.AppCompat" android:textAppearance="@style/TextAppearance.AppCompat"
android:textSize="36sp" android:textSize="36sp"
android:textAlignment="center" /> android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView <com.google.android.gms.common.SignInButton
android:text="Register"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="31dp" android:layout_marginTop="186dp"
android:id="@+id/regTextView" android:id="@+id/sign_in_button"
android:textAlignment="center" android:layout_centerHorizontal="true"></com.google.android.gms.common.SignInButton>
android:layout_below="@+id/loginButton"
android:layout_centerHorizontal="true" />
</RelativeLayout> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_registration" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.darshanbshah.odsystem.MainActivity">
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.darshanbshah.odsystem.Registration">
<Button
android:text="Sign Out"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sign_out_button"
android:onClick="signOut"
style="@android:style/Widget.Material.Button.ButtonBar.AlertDialog"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
</RelativeLayout> </RelativeLayout>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment