Improved authentication; added fallbacks to 3D; cleaner dashboard charts
This commit is contained in:
@@ -41,7 +41,8 @@ interface GoogleToken extends JWT {
|
||||
|
||||
async function refreshAccessToken(token: GoogleToken): Promise<GoogleToken> {
|
||||
try {
|
||||
const response = await fetch(`${process.env.BACKEND_URL}/auth/refresh/`, {
|
||||
const BACKEND = process.env.BACKEND_URL || 'http://127.0.0.1:8000/api/v1'
|
||||
const response = await fetch(`${BACKEND}/auth/refresh/`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -52,8 +53,14 @@ async function refreshAccessToken(token: GoogleToken): Promise<GoogleToken> {
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json()
|
||||
throw errorData
|
||||
const errorText = await response.text()
|
||||
let errorData: { error?: string; [key: string]: unknown } = {}
|
||||
try {
|
||||
errorData = JSON.parse(errorText)
|
||||
} catch {
|
||||
errorData = { error: errorText }
|
||||
}
|
||||
throw new Error(errorData.error || 'Token refresh failed')
|
||||
}
|
||||
|
||||
const refreshedTokens = await response.json()
|
||||
@@ -85,7 +92,8 @@ export const authOptions: NextAuthOptions = {
|
||||
},
|
||||
async authorize(credentials) {
|
||||
try {
|
||||
const res = await fetch(`${process.env.BACKEND_URL}/auth/login/`, {
|
||||
const BACKEND = process.env.BACKEND_URL || 'http://127.0.0.1:8000/api/v1'
|
||||
const res = await fetch(`${BACKEND}/auth/login/`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
@@ -94,18 +102,34 @@ export const authOptions: NextAuthOptions = {
|
||||
}),
|
||||
})
|
||||
|
||||
const data = await res.json()
|
||||
const raw = await res.text()
|
||||
let data: {
|
||||
error?: string;
|
||||
user?: {
|
||||
id: string | number;
|
||||
email: string;
|
||||
name: string;
|
||||
};
|
||||
access?: string;
|
||||
refresh?: string;
|
||||
[key: string]: unknown
|
||||
}
|
||||
try {
|
||||
data = JSON.parse(raw)
|
||||
} catch {
|
||||
data = { error: raw }
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(data.error || 'Authentication failed')
|
||||
throw new Error(data.error || `Authentication failed (${res.status})`)
|
||||
}
|
||||
|
||||
return {
|
||||
id: data.user.id.toString(),
|
||||
email: data.user.email,
|
||||
name: data.user.firstName,
|
||||
accessToken: data.access,
|
||||
refreshToken: data.refresh,
|
||||
id: data.user?.id?.toString?.() ?? String(data.user?.id),
|
||||
email: data.user?.email ?? '',
|
||||
name: data.user?.name ?? '', // backend uses `name`, not `firstName`
|
||||
accessToken: data.access ?? '',
|
||||
refreshToken: data.refresh ?? '',
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Login error:', error)
|
||||
|
||||
Reference in New Issue
Block a user