| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- import axios from 'axios';
- import { Alert } from 'react-native';
- import * as SecureStore from 'expo-secure-store';
- import { EXPO_PUBLIC_API_URL } from '@env';
- import { forgetPasswordFormData } from '../types/signup';
- import { CustomerData } from '../types/signUpFormData';
- class AuthenticationService {
- private apiUrl: string;
- constructor() {
- this.apiUrl = EXPO_PUBLIC_API_URL;
- if (!this.apiUrl) {
- throw new Error('API URL is not defined in environment variables');
- }
- }
- async login(username: string, password: string) {
- try {
- const response = await axios.post(
- `${this.apiUrl}/public/client/customer/sign-in`,
- {
- email: username,
- password: password
- },
- {
- headers: {
- 'Content-Type': 'application/json',
- Accept: 'application/json'
- }
- }
- );
- if (response.status === 201) {
- const token = response.data.accessToken;
- await SecureStore.setItemAsync('accessToken', token);
- console.log('Login successful!');
- return true;
- } else {
- console.error('Login failed:', response.status);
- return false;
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Login error:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- return false;
- }
- }
- async logout() {
- await SecureStore.deleteItemAsync('accessToken');
- console.log('log out successfully, accessToken deleted');
- }
- //BELOW CODES RELATE TO "SIGN UP"
- //BELOW CODES RELATE TO "SIGN UP"
- async sendOtpToSignUpEmail(email: string) {
- try {
- const response = await axios.post(
- `${this.apiUrl}/public/client/customer/otp`,
- { email: email },
- {
- headers: {
- 'Content-Type': 'application/json',
- Accept: 'application/json'
- }
- }
- );
- if (response.status === 200 || response.status === 201) {
- console.log('OTP sent successfully');
- return true;
- } else {
- console.error('Failed to send OTP:', response.status);
- return false;
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Error sending OTP:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error(
- 'An unexpected error occurred while sending OTP:',
- error
- );
- }
- return false;
- }
- }
- async verifySignUpOtp(
- email: string,
- code: string,
- setScreen: React.Dispatch<React.SetStateAction<number>>,
- setError: React.Dispatch<React.SetStateAction<string>>
- ) {
- try {
- const response = await axios.put(
- `${this.apiUrl}/public/client/customer/otp`,
- { email, code },
- {
- headers: {
- 'Content-Type': 'application/json',
- Accept: 'application/json'
- }
- }
- );
- if (response.status === 200) {
- console.log('OTP verified successfully');
- setScreen(
- (currentScreenNumber: number) => currentScreenNumber + 1
- );
- return true;
- } else {
- console.error('OTP verification failed:', response.status);
- setError('OTP驗證碼錯誤');
- return false;
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Error verifying OTP:',
- error.response?.data?.message || error.message
- );
- setError(
- '發生意外錯誤,請確保您輸入的電子郵件正確,並再次確認您的OTP驗證碼是否正確。'
- );
- } else {
- console.error(
- 'An unexpected error occurred while verifying OTP:',
- error
- );
- setError('發生意外錯誤,請稍後再試');
- }
- return false;
- }
- }
- async signUp(data: CustomerData) {
- try {
- const response = await axios.post(
- `${this.apiUrl}/public/client/customer`,
- data,
- {
- headers: {
- 'Content-Type': 'application/json',
- Accept: 'application/json'
- }
- }
- );
- if (response.status === 200 || response.status === 201) {
- console.log('Signup successful');
- return true;
- } else {
- console.error('Signup failed:', response.status);
- return false;
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Error signing up:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error(
- 'An unexpected error occurred while signing up:',
- error
- );
- }
- return false;
- }
- }
- //BELOW CODES RELATE TO "FORGET PASSWORD"
- //BELOW CODES RELATE TO "FORGET PASSWORD"
- async sendForgetPasswordOtp(email: string) {
- try {
- const response = await axios.post(
- `${this.apiUrl}/public/client/customer/pw/otp`,
- { email },
- {
- headers: {
- 'Content-Type': 'application/json',
- Accept: 'application/json'
- }
- }
- );
- if (response.status === 200 || response.status === 201) {
- console.log('Forget password OTP sent successfully');
- return true;
- } else {
- console.error(
- 'Failed to send forget password OTP:',
- response.status
- );
- return false;
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Error sending forget password OTP:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error(
- 'An unexpected error occurred while sending forget password OTP:',
- error
- );
- }
- return false;
- }
- }
- async verifyingOtpForgetPassword(
- email: string,
- otp: string,
- setForgetPasswordFormData: React.Dispatch<
- React.SetStateAction<forgetPasswordFormData>
- >,
- setData: React.Dispatch<React.SetStateAction<string>>
- ) {
- try {
- const res = await axios.put(
- `${this.apiUrl}/public/client/customer/pw/otp`,
- {
- email: email,
- code: otp
- }
- );
- const data = res.data;
- setData(data.msg);
- console.log(data.msg);
- setForgetPasswordFormData((prevFormData) => ({
- ...prevFormData,
- otpAuthCompleted: true
- }));
- return true;
- } catch (error) {
- console.error('Error', error);
- return false;
- }
- }
- async changePassword(confirmedNewPassword: string, data: string) {
- try {
- const res = await axios.put(
- `${this.apiUrl}/clients/customer/pw/forget`,
- { newPassword: confirmedNewPassword },
- {
- headers: {
- Authorization: `Bearer ${data}`
- }
- }
- );
- return true;
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Error changing password:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- }
- }
- //BELOW CODES RELATE TO "changing account info (such as gender, name)"
- //BELOW CODES RELATE TO "changing account info (such as gender, name)"
- async changeName(
- name: string | null,
- token: string | null
- ): Promise<boolean> {
- try {
- const res = await axios.put(
- `${this.apiUrl}/clients/customer`,
- { nickname: name },
- {
- headers: {
- Authorization: `Bearer ${token}`
- }
- }
- );
- console.log('Change Name Successfully!');
- return true;
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Error changing name:',
- error.response?.data?.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- return false;
- }
- }
- async changePhone(
- phone: string | null,
- token: string | null
- ): Promise<boolean> {
- try {
- const convertPhoneStringToNumber = Number(phone);
- const res = await axios.put(
- `${this.apiUrl}/clients/customer`,
- { phone: convertPhoneStringToNumber },
- {
- headers: {
- Authorization: `Bearer ${token}`
- }
- }
- );
- console.log('Change Phone Successfully!');
- return true;
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Error changing phone:',
- error.response?.data?.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- return false;
- }
- }
- async changeGender(
- gender: string | null,
- token: string | null
- ): Promise<boolean> {
- try {
- const res = await axios.put(
- `${this.apiUrl}/clients/customer`,
- { gender: gender },
- {
- headers: {
- Authorization: `Bearer ${token}`
- }
- }
- );
- console.log('Change gender Successfully!');
- return true;
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Error changing gender:',
- error.response?.data?.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- return false;
- }
- }
- async getUserInfo() {
- try {
- const response = await axios.get(
- `${this.apiUrl}/clients/customer`,
- {
- headers: {
- Authorization: `Bearer ${await SecureStore.getItemAsync(
- 'accessToken'
- )}`
- }
- }
- );
- if (response.status === 200 || response.status === 201) {
- // console.log(response);
- return response;
- } else {
- console.log('invalid response');
- }
- } catch (error) {
- console.log(error);
- }
- }
- }
- export const authenticationService = new AuthenticationService();
|