Get token from user

This commit is contained in:
Nezumi
2023-03-11 16:18:41 +07:00
parent 5905fbf5a4
commit e7e49ec4e3
8 changed files with 138 additions and 7 deletions
+11
View File
@@ -1,5 +1,6 @@
plugins { plugins {
id 'com.android.application' id 'com.android.application'
id 'com.google.gms.google-services'
} }
android { android {
@@ -32,6 +33,12 @@ android {
targetCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8
} }
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
}
}
} }
@@ -39,6 +46,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.firebase:firebase-auth:21.1.0'
testImplementation 'junit:junit:4.13.2' testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.ext:junit:1.1.5'
@@ -63,4 +71,7 @@ dependencies {
implementation 'com.github.slavce14:SJ-Dialog:1.5' implementation 'com.github.slavce14:SJ-Dialog:1.5'
implementation 'com.saadahmedev.popup-dialog:popup-dialog:1.0.2' implementation 'com.saadahmedev.popup-dialog:popup-dialog:1.0.2'
implementation 'com.github.gabriel-TheCode:AestheticDialogs:1.3.6' implementation 'com.github.gabriel-TheCode:AestheticDialogs:1.3.6'
//Firebase
implementation platform('com.google.firebase:firebase-bom:31.2.3')
} }
+40
View File
@@ -0,0 +1,40 @@
{
"project_info": {
"project_number": "213676556381",
"firebase_url": "https://foodify-55954-default-rtdb.firebaseio.com",
"project_id": "foodify-55954",
"storage_bucket": "foodify-55954.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:213676556381:android:b1fb5bb1a827c24ce88db4",
"android_client_info": {
"package_name": "com.capstone.foodify"
}
},
"oauth_client": [
{
"client_id": "213676556381-jmh1dva918r6juc331d1jclgjeinqor1.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyBBboME-vz27_Rgg98ftivvSvMVoH9lV1A"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "213676556381-jmh1dva918r6juc331d1jclgjeinqor1.apps.googleusercontent.com",
"client_type": 3
}
]
}
}
}
],
"configuration_version": "1"
}
@@ -17,6 +17,7 @@ import com.capstone.foodify.R;
import com.capstone.foodify.ViewPagerAdapter; import com.capstone.foodify.ViewPagerAdapter;
import com.google.android.material.bottomnavigation.BottomNavigationView; import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView; import com.google.android.material.navigation.NavigationBarView;
import com.google.firebase.FirebaseApp;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
@@ -31,6 +32,7 @@ public class MainActivity extends AppCompatActivity {
SplashScreen.installSplashScreen(this); SplashScreen.installSplashScreen(this);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
initComponent(); initComponent();
bottomNavigation(); bottomNavigation();
@@ -1,32 +1,56 @@
package com.capstone.foodify.Activity; package com.capstone.foodify.Activity;
import android.content.Intent; import android.content.Intent;
import android.graphics.Path;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.capstone.foodify.Common; import com.capstone.foodify.Common;
import com.capstone.foodify.R; import com.capstone.foodify.R;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout; import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GetTokenResult;
public class SignInActivity extends AppCompatActivity { public class SignInActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
TextView signUp_textView, forgotPassword_textView; TextView signUp_textView, forgotPassword_textView;
TextInputLayout textInput_account, textInput_password; TextInputLayout textInput_account, textInput_password;
TextInputEditText email, password;
MaterialButton signInButton;
ImageView back_image; ImageView back_image;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in); //Init firebase app
initComponent(); FirebaseApp.initializeApp(SignInActivity.this);
setContentView(R.layout.activity_sign_in);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
initComponent();
setFontUI(); setFontUI();
back_image.setOnClickListener(new View.OnClickListener() { back_image.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
@@ -48,13 +72,54 @@ public class SignInActivity extends AppCompatActivity {
startActivity(new Intent(SignInActivity.this, ForgotPasswordActivity.class)); startActivity(new Intent(SignInActivity.this, ForgotPasswordActivity.class));
} }
}); });
//Set event when user click sign in button
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAuth.signInWithEmailAndPassword(email.getText().toString(), password.getText().toString())
.addOnCompleteListener(SignInActivity.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
FirebaseUser user = mAuth.getCurrentUser();
user.getIdToken(true)
.addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
@Override
public void onComplete(@NonNull Task<GetTokenResult> task) {
if(task.isSuccessful()){
String idToken = task.getResult().getToken();
System.out.println("TOKEN: " + idToken);
} else {
Toast.makeText(SignInActivity.this, "Error when taking token!", Toast.LENGTH_SHORT).show();
} }
}
});
} else {
// If sign in fails, display a message to the user.
Toast.makeText(SignInActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
private void initComponent() { private void initComponent() {
textInput_account = (TextInputLayout) findViewById(R.id.textInput_account); textInput_account = (TextInputLayout) findViewById(R.id.textInput_account);
textInput_password = (TextInputLayout) findViewById(R.id.textInput_password); textInput_password = (TextInputLayout) findViewById(R.id.textInput_password);
signUp_textView = (TextView) findViewById(R.id.sign_up_textView); signUp_textView = (TextView) findViewById(R.id.sign_up_textView);
forgotPassword_textView = (TextView) findViewById(R.id.forgotPassword_textView); forgotPassword_textView = (TextView) findViewById(R.id.forgotPassword_textView);
back_image = (ImageView) findViewById(R.id.back_image); back_image = (ImageView) findViewById(R.id.back_image);
email = (TextInputEditText) findViewById(R.id.edt_email);
password = (TextInputEditText) findViewById(R.id.edt_password);
signInButton = (MaterialButton) findViewById(R.id.sign_in_button);
} }
private void setFontUI() { private void setFontUI() {
textInput_account.setTypeface(Common.setFontBebas(getAssets())); textInput_account.setTypeface(Common.setFontBebas(getAssets()));
@@ -13,12 +13,13 @@ import androidx.fragment.app.Fragment;
import com.capstone.foodify.Activity.AccountAndProfileActivity; import com.capstone.foodify.Activity.AccountAndProfileActivity;
import com.capstone.foodify.Activity.AddressManagerActivity; import com.capstone.foodify.Activity.AddressManagerActivity;
import com.capstone.foodify.Activity.OrderActivity; import com.capstone.foodify.Activity.OrderActivity;
import com.capstone.foodify.Activity.SignInActivity;
import com.capstone.foodify.Activity.SignUpActivity; import com.capstone.foodify.Activity.SignUpActivity;
import com.capstone.foodify.R; import com.capstone.foodify.R;
public class ProfileFragment extends Fragment { public class ProfileFragment extends Fragment {
Button btnSignUp; Button btnSignUp, btnSignIn;
LinearLayout account_and_profile, manage_address, order_history; LinearLayout account_and_profile, manage_address, order_history;
@Override @Override
@@ -30,6 +31,7 @@ public class ProfileFragment extends Fragment {
account_and_profile = rootView.findViewById(R.id.account_and_profile); account_and_profile = rootView.findViewById(R.id.account_and_profile);
manage_address = rootView.findViewById(R.id.manage_address); manage_address = rootView.findViewById(R.id.manage_address);
order_history = rootView.findViewById(R.id.order_history); order_history = rootView.findViewById(R.id.order_history);
btnSignIn = rootView.findViewById(R.id.sign_in_button);
btnSignUp.setOnClickListener(new View.OnClickListener() { btnSignUp.setOnClickListener(new View.OnClickListener() {
@Override @Override
@@ -58,6 +60,13 @@ public class ProfileFragment extends Fragment {
startActivity(new Intent(getActivity(), OrderActivity.class)); startActivity(new Intent(getActivity(), OrderActivity.class));
} }
}); });
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getContext().startActivity(new Intent(getContext(), SignInActivity.class));
}
});
return rootView; return rootView;
} }
} }
+1 -1
View File
@@ -102,7 +102,7 @@
app:layout_constraintTop_toBottomOf="@+id/textInput_password" /> app:layout_constraintTop_toBottomOf="@+id/textInput_password" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/sign_up_button" android:id="@+id/sign_in_button"
style="@style/DefaultSolid" style="@style/DefaultSolid"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
+2 -2
View File
@@ -14,7 +14,7 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
android:visibility="visible" android:visibility="gone"
> >
<com.makeramen.roundedimageview.RoundedImageView <com.makeramen.roundedimageview.RoundedImageView
@@ -227,7 +227,7 @@
android:id="@+id/guest_layout" android:id="@+id/guest_layout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:visibility="gone" android:visibility="visible"
> >
<com.makeramen.roundedimageview.RoundedImageView <com.makeramen.roundedimageview.RoundedImageView
+5 -1
View File
@@ -1,4 +1,8 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.15'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins { plugins {
id 'com.android.application' version '7.4.2' apply false id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false id 'com.android.library' version '7.4.2' apply false