LogIn and register works!

This commit is contained in:
Marijn Jansen
2017-06-15 12:48:13 +02:00
parent a2902e4546
commit 3bad43e3a5
10 changed files with 317 additions and 60 deletions

View File

@@ -28,9 +28,11 @@ dependencies {
compile 'com.android.support:design:25.3.1'
compile 'com.google.firebase:firebase-database:10.0.1'
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

View File

@@ -1,26 +0,0 @@
package nl.myhyvesbookplus.stagram;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("nl.myhyvesbookplus.stagram", appContext.getPackageName());
}
}

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="nl.myhyvesbookplus.stagram">
package="nl.myhyvesbookplus.tagram">
<application
android:allowBackup="true"
@@ -10,8 +10,11 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:name="nl.myhyvesbookplus.tagram.MainActivity"
android:label="@string/app_name">
</activity>
<activity android:name="nl.myhyvesbookplus.tagram.LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@@ -0,0 +1,190 @@
package nl.myhyvesbookplus.tagram;
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.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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;
public class LoginActivity extends AppCompatActivity {
public static final String TAG = "Login";
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(nl.myhyvesbookplus.tagram.R.layout.activity_login);
mAuth = FirebaseAuth.getInstance();
}
private void registerUser(String email, String password) {
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUserInfo(user);
goToMainScreen();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
// updateUI(null);
}
// ...
}
});
}
public void logInOnClick(View view) {
mAuth.signOut();
EditText email = (EditText) findViewById(R.id.email);
EditText password = (EditText) findViewById(R.id.password);
String emailSting = email.getText().toString();
String passwordSting = password.getText().toString();
mAuth.signInWithEmailAndPassword(emailSting, passwordSting)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
Log.d(TAG, "onComplete: isVerified " + user.isEmailVerified());
user.isEmailVerified();
goToMainScreen();
// updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithEmail:failure", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
// updateUI(null);
}
// ...
}
});
}
public void registerOnClick(View view) {
// goto Register page.
Log.d(TAG, "registerOnClick: ");
EditText email = (EditText) findViewById(R.id.email);
EditText password = (EditText) findViewById(R.id.password);
EditText passwordConfirm = (EditText) findViewById(R.id.confirm_password_field);
if (Patterns.EMAIL_ADDRESS.matcher(email.getText().toString()).matches()) {
if (password.getText().toString().equals(passwordConfirm.getText().toString())) {
registerUser(email.getText().toString(), password.getText().toString());
} else {
Log.d(TAG, "registerOnClick: Passwords do not match");
}
} else {
Log.d(TAG, "registerOnClick: Not An Email-address");
}
}
public void goToRegisterOnClick(View view) {
EditText passwordConfirm = (EditText) findViewById(R.id.confirm_password_field);
TextView passwordConfirmLabel = (TextView) findViewById(R.id.confirm_password_label);
Button register = (Button) findViewById(R.id.register_button);
Button backToLogin = (Button) findViewById(R.id.back_to_login_button);
EditText username = (EditText) findViewById(R.id.username);
TextView usernameLabel = (TextView) findViewById(R.id.username_label);
passwordConfirm.setVisibility(View.VISIBLE);
passwordConfirmLabel.setVisibility(View.VISIBLE);
register.setVisibility(View.VISIBLE);
backToLogin.setVisibility(View.VISIBLE);
username.setVisibility(View.VISIBLE);
usernameLabel.setVisibility(View.VISIBLE);
Button goToRegister = (Button) findViewById(R.id.go_to_register_button);
Button logInButton = (Button) findViewById(R.id.login_button);
goToRegister.setVisibility(View.GONE);
logInButton.setVisibility(View.GONE);
}
public void backToLogin(View view) {
EditText passwordConfirm = (EditText) findViewById(R.id.confirm_password_field);
TextView passwordConfirmLabel = (TextView) findViewById(R.id.confirm_password_label);
Button register = (Button) findViewById(R.id.register_button);
Button backToLogin = (Button) findViewById(R.id.back_to_login_button);
EditText username = (EditText) findViewById(R.id.username);
TextView usernameLabel = (TextView) findViewById(R.id.username_label);
passwordConfirm.setVisibility(View.GONE);
passwordConfirmLabel.setVisibility(View.GONE);
register.setVisibility(View.GONE);
backToLogin.setVisibility(View.GONE);
username.setVisibility(View.GONE);
usernameLabel.setVisibility(View.GONE);
Button goToRegister = (Button) findViewById(R.id.go_to_register_button);
Button logInButton = (Button) findViewById(R.id.login_button);
goToRegister.setVisibility(View.VISIBLE);
logInButton.setVisibility(View.VISIBLE);
}
protected void sendConfirmEmail(FirebaseUser user) {
user.sendEmailVerification()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "Email sent.");
// goToMainScreen();
}
}
});
}
protected void goToMainScreen() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
protected void updateUserInfo(final FirebaseUser user) {
EditText username = (EditText) findViewById(R.id.username);
UserProfileChangeRequest request = new UserProfileChangeRequest.Builder()
.setDisplayName(username.getText().toString())
.build();
user.updateProfile(request)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "User profile updated.");
}
sendConfirmEmail(user);
}
});
}
}

View File

@@ -1,4 +1,4 @@
package nl.myhyvesbookplus.stagram;
package nl.myhyvesbookplus.tagram;
import android.os.Bundle;
import android.support.annotation.NonNull;
@@ -20,14 +20,14 @@ public class MainActivity extends AppCompatActivity {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
case nl.myhyvesbookplus.tagram.R.id.navigation_home:
mTextMessage.setText(nl.myhyvesbookplus.tagram.R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
case nl.myhyvesbookplus.tagram.R.id.navigation_dashboard:
mTextMessage.setText(nl.myhyvesbookplus.tagram.R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
case nl.myhyvesbookplus.tagram.R.id.navigation_notifications:
mTextMessage.setText(nl.myhyvesbookplus.tagram.R.string.title_notifications);
return true;
}
return false;
@@ -38,10 +38,10 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setContentView(nl.myhyvesbookplus.tagram.R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
mTextMessage = (TextView) findViewById(nl.myhyvesbookplus.tagram.R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(nl.myhyvesbookplus.tagram.R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference();

View File

@@ -0,0 +1,105 @@
<?xml version="1.0" encoding="utf-8"?><!--<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"--><!--xmlns:app="http://schemas.android.com/apk/res-auto"--><!--xmlns:tools="http://schemas.android.com/tools"--><!--android:layout_width="match_parent"--><!--android:layout_height="match_parent"--><!--tools:context="nl.myhyvesbookplus.stagram.LoginActivity">-->
<!--</android.support.constraint.ConstraintLayout>-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".LoginActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email" />
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/username_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Username"
android:visibility="gone" />
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:visibility="gone" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
<TextView
android:id="@+id/confirm_password_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirm Password"
android:visibility="gone" />
<EditText
android:id="@+id/confirm_password_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="confirm password"
android:inputType="textPassword"
android:visibility="gone" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="logInOnClick"
android:text="Login" />
<Button
android:id="@+id/go_to_register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="goToRegisterOnClick"
android:text="Register" />
<Button
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="registerOnClick"
android:text="Register"
android:visibility="gone" />
<Button
android:id="@+id/back_to_login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:onClick="backToLogin"
android:text="back to Login"
android:visibility="gone" />
</LinearLayout>

View File

@@ -6,7 +6,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="nl.myhyvesbookplus.stagram.MainActivity">
tools:context="nl.myhyvesbookplus.tagram.MainActivity">
<FrameLayout
android:id="@+id/content"

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorPrimary">#fbbf2d</color>
<color name="colorPrimaryDark">#ffa70f</color>
<color name="colorAccent">#4CAF50</color>
</resources>

View File

@@ -1,5 +1,5 @@
<resources>
<string name="app_name">MyHyvesBook+Stagram</string>
<string name="app_name">MyHyvesBookPlusTagram</string>
<string name="title_home">Home</string>
<string name="title_dashboard">Dashboard</string>
<string name="title_notifications">Notifications</string>

View File

@@ -1,17 +0,0 @@
package nl.myhyvesbookplus.stagram;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}