fix(auth): adjust secure cookie flag for HTTP/HTTPS and fallback Bearer token
This commit is contained in:
parent
0f22643707
commit
562c3f9ca1
@ -37,8 +37,10 @@ class ApiClient {
|
|||||||
async request(endpoint, options = {}) {
|
async request(endpoint, options = {}) {
|
||||||
const url = `${this.baseUrl}${endpoint.startsWith('/') ? endpoint : `/${endpoint}`}`;
|
const url = `${this.baseUrl}${endpoint.startsWith('/') ? endpoint : `/${endpoint}`}`;
|
||||||
|
|
||||||
|
const token = typeof sessionStorage !== 'undefined' ? sessionStorage.getItem('a2host_token') : null;
|
||||||
const headers = {
|
const headers = {
|
||||||
'Accept': 'application/json, application/problem+json',
|
'Accept': 'application/json, application/problem+json',
|
||||||
|
...(token ? { 'Authorization': `Bearer ${token}` } : {}),
|
||||||
...(options.headers || {})
|
...(options.headers || {})
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -95,16 +97,26 @@ class ApiClient {
|
|||||||
|
|
||||||
// --- Auth Endpoints ---
|
// --- Auth Endpoints ---
|
||||||
async login(credentials) {
|
async login(credentials) {
|
||||||
return this.request('/auth/login', {
|
const res = await this.request('/auth/login', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: credentials
|
body: credentials
|
||||||
});
|
});
|
||||||
|
if (res && res.token && typeof sessionStorage !== 'undefined') {
|
||||||
|
sessionStorage.setItem('a2host_token', res.token);
|
||||||
|
}
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
async logout() {
|
async logout() {
|
||||||
return this.request('/auth/logout', {
|
try {
|
||||||
|
await this.request('/auth/logout', {
|
||||||
method: 'POST'
|
method: 'POST'
|
||||||
});
|
});
|
||||||
|
} finally {
|
||||||
|
if (typeof sessionStorage !== 'undefined') {
|
||||||
|
sessionStorage.removeItem('a2host_token');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async getMe() {
|
async getMe() {
|
||||||
|
|||||||
@ -41,7 +41,7 @@ export class AuthController {
|
|||||||
sameSite: 'Strict',
|
sameSite: 'Strict',
|
||||||
priority: 'High',
|
priority: 'High',
|
||||||
maxAge: maxAgeSeconds,
|
maxAge: maxAgeSeconds,
|
||||||
secure: config.isProduction
|
secure: Boolean(req.secure || req.headers['x-forwarded-proto'] === 'https')
|
||||||
});
|
});
|
||||||
|
|
||||||
res.setHeader('Set-Cookie', cookieHeader);
|
res.setHeader('Set-Cookie', cookieHeader);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user