Skip to main content

Xác thực

Hình thức

Xác thực trong ứng dụng gồm 3 hình thức:

  • Đăng nhập với tài khoản đã đăng ký.
  • Đăng nhập bằng tài khoản Google (xác thực 2 lớp: mật khẩu và điện thoại) tiêu chuẩn Oauth2.
  • Đăng nhập bằng tài khoản Microsoft (xác thực 2 lớp: mật khẩu và điện thoại) tiêu chuẩn Oauth2.

Tiêu chuẩn Oauth2

/src/api/AuthenticateService.ts
import AsyncStorage from "@react-native-async-storage/async-storage";
import { CFCToken } from "../model/authenticate/CFCToken";
import { ChangePasswordPutModel } from "../model/authenticate/ChangePasswordPutModel";
import { GoogleSignInResponse } from "../model/authenticate/GoogleSignInResponse";
import { MicrosoftCompleteSignInPostModel } from "../model/authenticate/MicrosoftCompleteSignInPostModel";
import { TokenRequestModel } from "../model/authenticate/TokenRequestModel";
import { ApiResponse } from "../model/base/ApiResponseModel";
import { HttpService } from "./HttpService";
import { ProfileResponseModel } from "../model/profiles/ProfileResponseModel";

class AuthenticateApi extends HttpService {
constructor() {
super();
this.baseurl = this.publicIdApiUrl;
}
NormalLogin = async (loginModel: TokenRequestModel) => {
const res = await this.Post(
"/api/id/v1/authentications/login/mobile-app",
loginModel
);
if (res.status !== 200) {
let result = new ApiResponse<CFCToken>();
result.statusCode = res.status;
return result;
}
const json: ApiResponse<CFCToken> = await res.json();
if (json && json.result) {
await AsyncStorage.setItem("token", json.result.accessToken!);
await AsyncStorage.setItem("refresh-token", json.result.refreshToken!);
await AsyncStorage.setItem("cfc-token", JSON.stringify(json.result));
}
return json;
};
MicrosoftLogin = async (stateCode: string, email: string) => {
let model = new MicrosoftCompleteSignInPostModel(stateCode, email);
console.log(model);
const res = await this.Post(
`/api/id/v1/authentications/microsoft-signin-fm-mobile-app`,
model
);
if (res.status !== 200) {
let result = new ApiResponse<CFCToken>();
result.statusCode = res.status;
return result;
}
const json: ApiResponse<CFCToken> = await res.json();
if (json && json.result) {
await AsyncStorage.setItem("token", json.result.accessToken!);
await AsyncStorage.setItem("refresh-token", json.result.refreshToken!);
await AsyncStorage.setItem("cfc-token", JSON.stringify(json.result));
}
return json;
};
GetMicrosoftAuthenticateUrl = async () => {
return `${this.publicIdApiUrl}/external/microsoft-signin-fm-mobile-app`;
};
GetNewToken = async () => {
let model = new TokenRequestModel();
model.refreshToken = (await this.GetRefreshToken())!;
const res = await this.Post(
`/api/id/v1/authentications/login/mobile-app`,
model
);
console.log(res);
if (res.status !== 200) {
let result = new ApiResponse<CFCToken>();
result.statusCode = res.status;
return result;
}
const json: ApiResponse<CFCToken> = await res.json();
if (json && json.result) {
await AsyncStorage.setItem("token", json.result.accessToken!);
await AsyncStorage.setItem("refresh-token", json.result.refreshToken!);
await AsyncStorage.setItem("cfc-token", JSON.stringify(json.result));
}
return json;
};
ChangePassword = async (model: ChangePasswordPutModel) => {
const res = await this.Put(
`/api/id/v1/my-accounts/fm-change-password`,
model
);
if (res.status !== 200) {
let result = new ApiResponse<boolean>();
result.statusCode = res.status;
return result;
}
const json: ApiResponse<boolean> = await res.json();
return json;
};

GoogleLoginIOS = async (model: GoogleSignInResponse) => {
const res = await this.Post(
`/api/id/v1/authentications/google/fm-ios`,
model
);
if (res.status !== 200) {
let result = new ApiResponse<CFCToken>();
result.statusCode = res.status;
return result;
}
const json: ApiResponse<CFCToken> = await res.json();
if (json && json.result) {
await AsyncStorage.setItem("token", json.result.accessToken!);
await AsyncStorage.setItem("refresh-token", json.result.refreshToken!);
await AsyncStorage.setItem("cfc-token", JSON.stringify(json.result));
}
return json;
};
GoogleLoginAndroid = async (model: GoogleSignInResponse) => {
const res = await this.Post(
`/api/id/v1/authentications/google/fm-android`,
model
);
if (res.status !== 200) {
let result = new ApiResponse<CFCToken>();
result.statusCode = res.status;
return result;
}
const json: ApiResponse<CFCToken> = await res.json();
if (json && json.result) {
await AsyncStorage.setItem("token", json.result.accessToken!);
await AsyncStorage.setItem("refresh-token", json.result.refreshToken!);
await AsyncStorage.setItem("cfc-token", JSON.stringify(json.result));
}
return json;
};
LinkGoogleIOS = async (model: GoogleSignInResponse) => {
const res = await this.Post(
`/api/id/v1/my-accounts/links/google-ios`,
model
);
if (res.status !== 200) {
let result = new ApiResponse<ProfileResponseModel>();
result.statusCode = res.status;
return result;
}
const json: ApiResponse<ProfileResponseModel> = await res.json();
return json;
};
LinkGoogleAndroid = async (model: GoogleSignInResponse) => {
const res = await this.Post(`/api/id/v1/my-accounts/links/google`, model);
if (res.status !== 200) {
let result = new ApiResponse<ProfileResponseModel>();
result.statusCode = res.status;
return result;
}
const json: ApiResponse<ProfileResponseModel> = await res.json();
return json;
};
Profiles = async () => {
const res = await this.Get("/api/id/v1/my-accounts/profiles");
if (res.status !== 200) {
let result = new ApiResponse<ProfileResponseModel>();
result.statusCode = res.status;
return result;
}
const json: ApiResponse<ProfileResponseModel> = await res.json();
return json;
};
Logout = async () => {
const res = await this.Post(`/api/id/v1/my-accounts/logout-fm-mobile`);
if (res.status !== 200) {
let result = new ApiResponse<boolean>();
result.statusCode = res.status;
return result;
}
const json: ApiResponse<boolean> = await res.json();
return json;
};
}
const authenticateApi = new AuthenticateApi();
export default authenticateApi;