update authentication logic to conditionally include credentials for native platforms

This commit is contained in:
Mo Tarbin
2026-02-08 10:29:39 -05:00
parent 86cfc10741
commit c2c1c6dfcc
2 changed files with 25 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
import { createContext, useContext, useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import { clearAllTokens, saveTokens } from '../utils/TokenStorage'
import { apiClient } from '../utils/ApiClient'
import { clearAllTokens, saveTokens } from '../utils/TokenStorage'
const AuthContext = createContext(null)
@@ -36,12 +36,21 @@ export const AuthProvider = ({ children }) => {
const login = async credentials => {
setIsLoading(true)
try {
const response = await fetch(`${baseURL}/auth/login`, {
const isNative =
typeof window !== 'undefined' && window.Capacitor?.isNativePlatform?.()
const config = {
method: 'POST',
credentials: 'include',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(credentials),
})
}
// Only use credentials for web, not for native apps
if (!isNative) {
config.credentials = 'include'
}
const response = await fetch(`${baseURL}/auth/login`, config)
if (!response.ok) {
const error = await response.json()

View File

@@ -68,10 +68,19 @@ const login = (username, password) => {
const logout = () => {
const baseURL = apiManager.getApiURL()
return fetch(`${baseURL}/auth/logout`, {
const isNative =
typeof window !== 'undefined' && window.Capacitor?.isNativePlatform?.()
const config = {
method: 'POST',
credentials: 'include',
})
}
// Only use credentials for web, not for native apps
if (!isNative) {
config.credentials = 'include'
}
return fetch(`${baseURL}/auth/logout`, config)
}
const GetAllUsers = () => {