update authentication logic to conditionally include credentials for native platforms
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { createContext, useContext, useEffect, useState } from 'react'
|
import { createContext, useContext, useEffect, useState } from 'react'
|
||||||
import { useNavigate } from 'react-router-dom'
|
import { useNavigate } from 'react-router-dom'
|
||||||
import { clearAllTokens, saveTokens } from '../utils/TokenStorage'
|
|
||||||
import { apiClient } from '../utils/ApiClient'
|
import { apiClient } from '../utils/ApiClient'
|
||||||
|
import { clearAllTokens, saveTokens } from '../utils/TokenStorage'
|
||||||
|
|
||||||
const AuthContext = createContext(null)
|
const AuthContext = createContext(null)
|
||||||
|
|
||||||
@@ -36,12 +36,21 @@ export const AuthProvider = ({ children }) => {
|
|||||||
const login = async credentials => {
|
const login = async credentials => {
|
||||||
setIsLoading(true)
|
setIsLoading(true)
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`${baseURL}/auth/login`, {
|
const isNative =
|
||||||
|
typeof window !== 'undefined' && window.Capacitor?.isNativePlatform?.()
|
||||||
|
|
||||||
|
const config = {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
credentials: 'include',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(credentials),
|
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) {
|
if (!response.ok) {
|
||||||
const error = await response.json()
|
const error = await response.json()
|
||||||
|
|||||||
@@ -68,10 +68,19 @@ const login = (username, password) => {
|
|||||||
|
|
||||||
const logout = () => {
|
const logout = () => {
|
||||||
const baseURL = apiManager.getApiURL()
|
const baseURL = apiManager.getApiURL()
|
||||||
return fetch(`${baseURL}/auth/logout`, {
|
const isNative =
|
||||||
|
typeof window !== 'undefined' && window.Capacitor?.isNativePlatform?.()
|
||||||
|
|
||||||
|
const config = {
|
||||||
method: 'POST',
|
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 = () => {
|
const GetAllUsers = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user