mirror of
https://github.com/Nezumi-2711/Foodify.git
synced 2026-09-22 20:00:50 +00:00
Get token from user
This commit is contained in:
@@ -17,6 +17,7 @@ import com.capstone.foodify.R;
|
||||
import com.capstone.foodify.ViewPagerAdapter;
|
||||
import com.google.android.material.bottomnavigation.BottomNavigationView;
|
||||
import com.google.android.material.navigation.NavigationBarView;
|
||||
import com.google.firebase.FirebaseApp;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
@@ -31,6 +32,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
SplashScreen.installSplashScreen(this);
|
||||
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
initComponent();
|
||||
|
||||
bottomNavigation();
|
||||
|
||||
@@ -1,32 +1,56 @@
|
||||
package com.capstone.foodify.Activity;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.graphics.Path;
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.capstone.foodify.Common;
|
||||
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.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 {
|
||||
|
||||
private FirebaseAuth mAuth;
|
||||
TextView signUp_textView, forgotPassword_textView;
|
||||
|
||||
TextInputLayout textInput_account, textInput_password;
|
||||
TextInputEditText email, password;
|
||||
MaterialButton signInButton;
|
||||
|
||||
ImageView back_image;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_sign_in);
|
||||
initComponent();
|
||||
//Init firebase app
|
||||
FirebaseApp.initializeApp(SignInActivity.this);
|
||||
|
||||
setContentView(R.layout.activity_sign_in);
|
||||
|
||||
|
||||
// Initialize Firebase Auth
|
||||
mAuth = FirebaseAuth.getInstance();
|
||||
|
||||
initComponent();
|
||||
setFontUI();
|
||||
|
||||
back_image.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
@@ -48,13 +72,54 @@ public class SignInActivity extends AppCompatActivity {
|
||||
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() {
|
||||
textInput_account = (TextInputLayout) findViewById(R.id.textInput_account);
|
||||
textInput_password = (TextInputLayout) findViewById(R.id.textInput_password);
|
||||
signUp_textView = (TextView) findViewById(R.id.sign_up_textView);
|
||||
forgotPassword_textView = (TextView) findViewById(R.id.forgotPassword_textView);
|
||||
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() {
|
||||
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.AddressManagerActivity;
|
||||
import com.capstone.foodify.Activity.OrderActivity;
|
||||
import com.capstone.foodify.Activity.SignInActivity;
|
||||
import com.capstone.foodify.Activity.SignUpActivity;
|
||||
import com.capstone.foodify.R;
|
||||
|
||||
public class ProfileFragment extends Fragment {
|
||||
|
||||
Button btnSignUp;
|
||||
Button btnSignUp, btnSignIn;
|
||||
|
||||
LinearLayout account_and_profile, manage_address, order_history;
|
||||
@Override
|
||||
@@ -30,6 +31,7 @@ public class ProfileFragment extends Fragment {
|
||||
account_and_profile = rootView.findViewById(R.id.account_and_profile);
|
||||
manage_address = rootView.findViewById(R.id.manage_address);
|
||||
order_history = rootView.findViewById(R.id.order_history);
|
||||
btnSignIn = rootView.findViewById(R.id.sign_in_button);
|
||||
|
||||
btnSignUp.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@@ -58,6 +60,13 @@ public class ProfileFragment extends Fragment {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -102,7 +102,7 @@
|
||||
app:layout_constraintTop_toBottomOf="@+id/textInput_password" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/sign_up_button"
|
||||
android:id="@+id/sign_in_button"
|
||||
style="@style/DefaultSolid"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
android:visibility="visible"
|
||||
android:visibility="gone"
|
||||
>
|
||||
|
||||
<com.makeramen.roundedimageview.RoundedImageView
|
||||
@@ -227,7 +227,7 @@
|
||||
android:id="@+id/guest_layout"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:visibility="gone"
|
||||
android:visibility="visible"
|
||||
>
|
||||
|
||||
<com.makeramen.roundedimageview.RoundedImageView
|
||||
|
||||
Reference in New Issue
Block a user