Friday 17 March 2017



Firebase Tutorial Authentication - Part 1









Main Activity:

package com.example.administrator.firebasetutorial;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
import com.onesignal.OneSignal;

public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private EditText email,password,name;
private Button signin, signup;
static String LoggedIn_User_Email;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mAuth = FirebaseAuth.getInstance(); // important Call

signin = (Button)findViewById(R.id.signin);
signup = (Button)findViewById(R.id.signup);
email = (EditText)findViewById(R.id.etEmail);
password = (EditText)findViewById(R.id.etPassword);
name = (EditText)findViewById(R.id.etName);

//Check if User is Already LoggedIn
if(mAuth.getCurrentUser() != null)
{
//User NOT logged In
finish();
startActivity(new Intent(getApplicationContext(),SignIn.class));
}




signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String getemail = email.getText().toString().trim();
String getepassword = password.getText().toString().trim();
callsignin(getemail,getepassword);

}
});

signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String getemail = email.getText().toString().trim();
String getepassword = password.getText().toString().trim();
callsignup(getemail,getepassword);

}
});

}

//Create Account
private void callsignup(String email,String password) {

mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("TESTING", "Sign up Successful" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Signed up Failed", Toast.LENGTH_SHORT).show();
}
else
{
userProfile();
Toast.makeText(MainActivity.this, "Created Account", Toast.LENGTH_SHORT).show();
Log.d("TESTING", "Created Account");
}
}
});
}

//Set UserDisplay Name
private void userProfile()
{
FirebaseUser user = mAuth.getCurrentUser();
if(user!= null)
{
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(name.getText().toString().trim())
//.setPhotoUri(Uri.parse("https://example.com/jane-q-user/profile.jpg")) // here you can set image link also.
.build();

user.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d("TESTING", "User profile updated.");
}
}
});
}
}


//Now start Sign In Process
//SignIn Process
private void callsignin(String email,String password) {

mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d("TESTING", "sign In Successful:" + task.isSuccessful());

// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Log.w("TESTING", "signInWithEmail:failed", task.getException());
Toast.makeText(MainActivity.this, "Failed", Toast.LENGTH_SHORT).show();
}
else {
Intent i = new Intent(MainActivity.this, SignIn.class);
finish();
startActivity(i);
}
}
});

}


}




Sign IN:

package com.example.administrator.firebasetutorial;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

/**
* Created by Administrator on 16-03-2017.
*/

public class SignIn extends AppCompatActivity {
Button signout,upload_bttn,showData,notification;
private FirebaseAuth mAuth;
TextView username;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);

mAuth = FirebaseAuth.getInstance(); // important Call
signout = (Button)findViewById(R.id.signout);
username = (TextView) findViewById(R.id.tvName);




//Again check if the user is Already Logged in or Not
if(mAuth.getCurrentUser() == null)
{
//User NOT logged In
finish();
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}

//Fetch the Display name of current User
FirebaseUser user = mAuth.getCurrentUser();

if (user != null) {
username.setText("Welcome, " + user.getDisplayName());
}


signout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAuth.signOut();
finish();
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
});
}
}

2 comments:

  1. can u please upload the file!

    ReplyDelete
  2. Thanku bro for Excellent tutorial !
    Bro can u send me a tutorial how to create a folder and upload a images as admin and user should should only view those images

    please mail me nithin3116@gmail.com

    ReplyDelete