9s-fe-core
Version:
Core functionalities for authentication, configuration, and repository management.
91 lines (90 loc) • 5.13 kB
JavaScript
;
'use client';
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.LoginView = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const auth_repository_1 = require("../auth_repository");
const base_storage_1 = __importDefault(require("../../utils/base_storage"));
const AuthProvider_1 = require("../AuthProvider");
const LoginView = ({ onLoginSuccess }) => {
const [email, setEmail] = (0, react_1.useState)('');
const [password, setPassword] = (0, react_1.useState)('');
const [error, setError] = (0, react_1.useState)(null);
const [isLoading, setIsLoading] = (0, react_1.useState)(false);
const { login } = (0, AuthProvider_1.useAuth)(); // Sử dụng hook useAuth để lấy hàm login
const handleLogin = () => __awaiter(void 0, void 0, void 0, function* () {
setError(null); // Reset error before API call
setIsLoading(true); // Show loading state
try {
const result = yield auth_repository_1.authRepository.login(email, password);
if (result.status === 200 && result.dataResponse) {
const loginResponse = result.dataResponse;
const { access_token } = loginResponse;
// Lưu token
base_storage_1.default.setToken(access_token);
// Gọi getUserProfile để lấy thông tin người dùng
try {
const userProfile = yield auth_repository_1.authRepository.getUserProfile();
if (userProfile.status === 200 && userProfile.dataResponse) {
// Kết hợp thông tin từ login và profile
const fullUserData = Object.assign(Object.assign({}, loginResponse), userProfile.dataResponse);
// Gọi hàm login từ AuthProvider để cập nhật trạng thái xác thực
login(fullUserData);
// Gọi callback onLoginSuccess nếu có
if (onLoginSuccess) {
onLoginSuccess(fullUserData);
}
}
else {
// Nếu không lấy được profile, vẫn đăng nhập với dữ liệu cơ bản
login(loginResponse);
if (onLoginSuccess) {
onLoginSuccess(loginResponse);
}
}
}
catch (profileError) {
console.error('Error fetching user profile:', profileError);
// Vẫn đăng nhập với dữ liệu cơ bản nếu không lấy được profile
login(loginResponse);
if (onLoginSuccess) {
onLoginSuccess(loginResponse);
}
}
}
else {
// Show error message from API
setError(result.message || 'An unknown error occurred'); // Ensure a string is passed
}
}
catch (error) {
if (error instanceof Error) {
setError(error.message);
}
else if (typeof error === 'string') {
setError(error);
}
else {
setError('An unknown error occurred');
}
}
finally {
setIsLoading(false); // Turn off loading state
}
});
return ((0, jsx_runtime_1.jsxs)("div", { className: "login-view", children: [(0, jsx_runtime_1.jsx)("h2", { children: "Login" }), (0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)("input", { type: "email", placeholder: "Email", value: email, onChange: (e) => setEmail(e.target.value) }) }), (0, jsx_runtime_1.jsx)("div", { children: (0, jsx_runtime_1.jsx)("input", { type: "password", placeholder: "Password", value: password, onChange: (e) => setPassword(e.target.value) }) }), (0, jsx_runtime_1.jsx)("button", { onClick: handleLogin, disabled: isLoading, children: isLoading ? 'Logging in...' : 'Login' }), error && (0, jsx_runtime_1.jsx)("div", { className: "error", children: error })] }));
};
exports.LoginView = LoginView;