67 lines
1.9 KiB
TypeScript

import { createAsyncThunk } from '@reduxjs/toolkit';
import { PartnerProfile } from '@app-types/index';
import { getProfile, loginApi, updateProfile, verifyOtpApi } from '@api';
import { tokenManager } from '@services';
export const loginWithPhone = createAsyncThunk(
'auth/loginWithPhone',
async (phone: string, { rejectWithValue }) => {
try {
const response = await loginApi(phone);
console.log(response);
return response;
} catch (error: unknown) {
const message = error instanceof Error ? error.message : 'Login failed';
return rejectWithValue(message);
}
},
);
export const verifyOtp = createAsyncThunk(
'auth/verifyOtp',
async (
{ phone, code, role }: { phone: string; code: string; role: string },
{ rejectWithValue },
) => {
try {
const response = await verifyOtpApi(phone, code, role);
// Persist tokens on successful verification
if (response.accessToken && response.refreshToken) {
await tokenManager.setTokens(
response.accessToken,
response.refreshToken,
);
}
return response;
} catch (error: unknown) {
const message =
error instanceof Error ? error.message : 'OTP verification failed';
return rejectWithValue(message);
}
},
);
export const getProfileThunk = createAsyncThunk<PartnerProfile, string>(
'auth/getProfile',
async (partnerId, { rejectWithValue }) => {
try {
return await getProfile(partnerId);
} catch (error: any) {
return rejectWithValue(error.message || 'Failed to get profile');
}
},
);
export const updateProfileThunk = createAsyncThunk<
PartnerProfile,
Partial<PartnerProfile>
>('auth/updateProfile', async (updates, { rejectWithValue }) => {
try {
return await updateProfile(updates);
} catch (error: any) {
return rejectWithValue(error.message || 'Failed to update profile');
}
});