Add auto-load guest data on login
This commit is contained in:
@@ -52,4 +52,19 @@ public class AuthController : ControllerBase
|
|||||||
|
|
||||||
return Ok(userInfo);
|
return Ok(userInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpGet("me/person")]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult> GetCurrentUserPerson()
|
||||||
|
{
|
||||||
|
var email = User.FindFirstValue(ClaimTypes.Email);
|
||||||
|
if (email == null)
|
||||||
|
return Unauthorized();
|
||||||
|
|
||||||
|
var person = await _authService.GetUserPersonAsync(email);
|
||||||
|
if (person == null)
|
||||||
|
return NotFound(new { message = "Your account is not linked to a person. Please contact an administrator." });
|
||||||
|
|
||||||
|
return Ok(person);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ using System.Text;
|
|||||||
using Hospitality.Backend.Configuration;
|
using Hospitality.Backend.Configuration;
|
||||||
using Hospitality.Backend.DTOs;
|
using Hospitality.Backend.DTOs;
|
||||||
using Hospitality.Domain.Entities;
|
using Hospitality.Domain.Entities;
|
||||||
|
using Hospitality.Infrastructure.Data;
|
||||||
using Microsoft.AspNetCore.Identity;
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Microsoft.IdentityModel.Tokens;
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
|
||||||
@@ -15,15 +17,18 @@ public class AuthService : IAuthService
|
|||||||
private readonly UserManager<ApplicationUser> _userManager;
|
private readonly UserManager<ApplicationUser> _userManager;
|
||||||
private readonly SignInManager<ApplicationUser> _signInManager;
|
private readonly SignInManager<ApplicationUser> _signInManager;
|
||||||
private readonly JwtSettings _jwtSettings;
|
private readonly JwtSettings _jwtSettings;
|
||||||
|
private readonly HospitalityDbContext _context;
|
||||||
|
|
||||||
public AuthService(
|
public AuthService(
|
||||||
UserManager<ApplicationUser> userManager,
|
UserManager<ApplicationUser> userManager,
|
||||||
SignInManager<ApplicationUser> signInManager,
|
SignInManager<ApplicationUser> signInManager,
|
||||||
IOptions<JwtSettings> jwtSettings)
|
IOptions<JwtSettings> jwtSettings,
|
||||||
|
HospitalityDbContext context)
|
||||||
{
|
{
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
_signInManager = signInManager;
|
_signInManager = signInManager;
|
||||||
_jwtSettings = jwtSettings.Value;
|
_jwtSettings = jwtSettings.Value;
|
||||||
|
_context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<LoginResponse?> LoginAsync(LoginRequest request)
|
public async Task<LoginResponse?> LoginAsync(LoginRequest request)
|
||||||
@@ -68,6 +73,37 @@ public class AuthService : IAuthService
|
|||||||
return new UserInfoResponse(user.Email!, roles.ToArray());
|
return new UserInfoResponse(user.Email!, roles.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<object?> GetUserPersonAsync(string email)
|
||||||
|
{
|
||||||
|
var user = await _userManager.FindByEmailAsync(email);
|
||||||
|
if (user == null || user.PersonId == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var person = await _context.People
|
||||||
|
.Include(p => p.Quotas)
|
||||||
|
.ThenInclude(q => q.Product)
|
||||||
|
.FirstOrDefaultAsync(p => p.Id == user.PersonId.Value);
|
||||||
|
|
||||||
|
if (person == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return new
|
||||||
|
{
|
||||||
|
name = person.Name,
|
||||||
|
email = person.Email,
|
||||||
|
qrCode = person.QrCode.ToString(),
|
||||||
|
quotas = person.Quotas.Select(q => new
|
||||||
|
{
|
||||||
|
productName = q.Product.Name,
|
||||||
|
productType = q.Product.Type.ToString(),
|
||||||
|
initialAmount = q.InitialAmount,
|
||||||
|
usedAmount = q.UsedAmount,
|
||||||
|
remainingAmount = q.RemainingAmount
|
||||||
|
}).ToList()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private string GenerateJwtToken(ApplicationUser user, string[] roles)
|
private string GenerateJwtToken(ApplicationUser user, string[] roles)
|
||||||
{
|
{
|
||||||
var claims = new List<Claim>
|
var claims = new List<Claim>
|
||||||
|
|||||||
@@ -7,4 +7,5 @@ public interface IAuthService
|
|||||||
Task<LoginResponse?> LoginAsync(LoginRequest request);
|
Task<LoginResponse?> LoginAsync(LoginRequest request);
|
||||||
Task<bool> RegisterAsync(RegisterRequest request);
|
Task<bool> RegisterAsync(RegisterRequest request);
|
||||||
Task<UserInfoResponse?> GetUserInfoAsync(string email);
|
Task<UserInfoResponse?> GetUserInfoAsync(string email);
|
||||||
|
Task<object?> GetUserPersonAsync(string email);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,92 +1,110 @@
|
|||||||
import { useState } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import { Card, Form, Button, ProgressBar, ListGroup } from 'react-bootstrap';
|
import { Container, Card, Alert, Spinner, ProgressBar } from 'react-bootstrap';
|
||||||
import { QRCodeSVG } from 'qrcode.react';
|
import { QRCodeSVG } from 'qrcode.react';
|
||||||
import { usePersonByQrCode } from '../../hooks/useQrCode';
|
import { api } from '../../lib/api';
|
||||||
|
|
||||||
|
interface Quota {
|
||||||
|
productName: string;
|
||||||
|
remainingAmount: number;
|
||||||
|
initialAmount: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PersonData {
|
||||||
|
name: string;
|
||||||
|
email: string;
|
||||||
|
qrCode: string;
|
||||||
|
quotas: Quota[];
|
||||||
|
}
|
||||||
|
|
||||||
export default function GuestQrPage() {
|
export default function GuestQrPage() {
|
||||||
const [qrCode, setQrCode] = useState('');
|
const [personData, setPersonData] = useState<PersonData | null>(null);
|
||||||
const [showQr, setShowQr] = useState(false);
|
const [loading, setLoading] = useState(true);
|
||||||
const { data: person } = usePersonByQrCode(qrCode);
|
const [error, setError] = useState('');
|
||||||
|
|
||||||
const handleSubmit = (e: React.FormEvent) => {
|
useEffect(() => {
|
||||||
e.preventDefault();
|
// Auto-load guest's own data on mount
|
||||||
setShowQr(true);
|
const fetchGuestData = async () => {
|
||||||
};
|
try {
|
||||||
|
setLoading(true);
|
||||||
|
setError('');
|
||||||
|
|
||||||
|
// Get current user's person data
|
||||||
|
const response = await api.get('/auth/me/person');
|
||||||
|
|
||||||
|
setPersonData(response.data);
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.response?.data?.message || 'Failed to load your information. Make sure your account is linked to a person.');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchGuestData();
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="row justify-content-center">
|
<Container className="py-4">
|
||||||
<div className="col-md-8 col-lg-6">
|
<h2 className="mb-4">My QR Code & Quotas</h2>
|
||||||
<h1 className="text-center mb-4">Guest View</h1>
|
|
||||||
|
|
||||||
{!showQr ? (
|
{loading && (
|
||||||
<Card className="shadow-sm">
|
<div className="text-center py-5">
|
||||||
<Card.Body>
|
<Spinner animation="border" role="status">
|
||||||
<Form onSubmit={handleSubmit}>
|
<span className="visually-hidden">Loading...</span>
|
||||||
<Form.Group className="mb-3">
|
</Spinner>
|
||||||
<Form.Label>Enter Your QR Code</Form.Label>
|
</div>
|
||||||
<Form.Control
|
)}
|
||||||
type="text"
|
|
||||||
size="lg"
|
{error && <Alert variant="danger">{error}</Alert>}
|
||||||
value={qrCode}
|
|
||||||
onChange={(e) => setQrCode(e.target.value)}
|
{personData && (
|
||||||
placeholder="Your QR code (GUID)"
|
<>
|
||||||
required
|
<Card className="mb-4 shadow-sm">
|
||||||
/>
|
<Card.Body className="text-center py-4">
|
||||||
</Form.Group>
|
<h4 className="mb-3">{personData.name}</h4>
|
||||||
<Button variant="primary" type="submit" className="w-100" size="lg">
|
{personData.email && <p className="text-muted mb-4">{personData.email}</p>}
|
||||||
Show My QR Code
|
|
||||||
</Button>
|
<div className="d-flex justify-content-center mb-3">
|
||||||
</Form>
|
<div className="p-3 bg-white border rounded">
|
||||||
|
<QRCodeSVG value={personData.qrCode} size={256} level="H" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<small className="text-muted d-block">
|
||||||
|
Show this QR code to staff for scanning
|
||||||
|
</small>
|
||||||
</Card.Body>
|
</Card.Body>
|
||||||
</Card>
|
</Card>
|
||||||
) : (
|
|
||||||
<Card className="shadow text-center">
|
|
||||||
<Card.Body className="p-4">
|
|
||||||
{person && (
|
|
||||||
<>
|
|
||||||
<h2 className="mb-4">{person.name}</h2>
|
|
||||||
|
|
||||||
<div className="d-flex justify-content-center mb-4">
|
<Card className="shadow-sm">
|
||||||
<div className="p-4 bg-white border border-3 rounded">
|
<Card.Header>
|
||||||
<QRCodeSVG value={qrCode} size={256} />
|
<h5 className="mb-0">My Quotas</h5>
|
||||||
|
</Card.Header>
|
||||||
|
<Card.Body>
|
||||||
|
{personData.quotas.length === 0 ? (
|
||||||
|
<p className="text-muted mb-0">No quotas assigned</p>
|
||||||
|
) : (
|
||||||
|
<div className="d-grid gap-3">
|
||||||
|
{personData.quotas.map((quota, index) => (
|
||||||
|
<div key={index}>
|
||||||
|
<div className="d-flex justify-content-between mb-2">
|
||||||
|
<strong>{quota.productName}</strong>
|
||||||
|
<span className="text-muted">
|
||||||
|
{quota.remainingAmount} / {quota.initialAmount} remaining
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<ProgressBar
|
||||||
|
now={(quota.remainingAmount / quota.initialAmount) * 100}
|
||||||
|
variant={quota.remainingAmount > 0 ? 'success' : 'danger'}
|
||||||
|
style={{ height: '8px' }}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
))}
|
||||||
|
</div>
|
||||||
<h4 className="mb-3">Your Quotas</h4>
|
|
||||||
<ListGroup className="text-start">
|
|
||||||
{person.quotas?.map((quota) => (
|
|
||||||
<ListGroup.Item key={quota.productId}>
|
|
||||||
<div className="d-flex justify-content-between align-items-center mb-2">
|
|
||||||
<div>
|
|
||||||
<h6 className="mb-0">{quota.productName}</h6>
|
|
||||||
<small className="text-muted">{quota.productType}</small>
|
|
||||||
</div>
|
|
||||||
<div className="text-end">
|
|
||||||
<h5 className="mb-0 text-primary">{quota.remainingAmount}</h5>
|
|
||||||
<small className="text-muted">of {quota.initialAmount} remaining</small>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
<ProgressBar
|
|
||||||
now={(quota.remainingAmount / quota.initialAmount) * 100}
|
|
||||||
variant="primary"
|
|
||||||
/>
|
|
||||||
</ListGroup.Item>
|
|
||||||
))}
|
|
||||||
</ListGroup>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
variant="link"
|
|
||||||
onClick={() => setShowQr(false)}
|
|
||||||
className="mt-3"
|
|
||||||
>
|
|
||||||
Enter Different Code
|
|
||||||
</Button>
|
|
||||||
</>
|
|
||||||
)}
|
)}
|
||||||
</Card.Body>
|
</Card.Body>
|
||||||
</Card>
|
</Card>
|
||||||
)}
|
</>
|
||||||
</div>
|
)}
|
||||||
</div>
|
</Container>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user