@sanvika/auth 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +15 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
// SanvikaAuthProvider.jsx
|
|
4
|
-
import { createContext, useContext, useEffect, useState } from "react";
|
|
4
|
+
import { createContext, useContext, useEffect, useState, useCallback } from "react";
|
|
5
5
|
|
|
6
6
|
// constants.js
|
|
7
7
|
var STORAGE_KEYS = {
|
|
@@ -82,14 +82,28 @@ function SanvikaAuthProvider({
|
|
|
82
82
|
setUser(null);
|
|
83
83
|
}
|
|
84
84
|
};
|
|
85
|
+
const authFetch = useCallback(async (url, opts = {}) => {
|
|
86
|
+
const currentToken = typeof window !== "undefined" ? localStorage.getItem(STORAGE_KEYS.ACCESS_TOKEN) : null;
|
|
87
|
+
const headers = {
|
|
88
|
+
"Content-Type": "application/json",
|
|
89
|
+
...currentToken && { Authorization: `Bearer ${currentToken}` },
|
|
90
|
+
...opts.headers
|
|
91
|
+
};
|
|
92
|
+
const res = await fetch(url, { ...opts, headers });
|
|
93
|
+
const data = await res.json();
|
|
94
|
+
return { data, success: data.success };
|
|
95
|
+
}, []);
|
|
85
96
|
const value = {
|
|
86
97
|
user,
|
|
87
98
|
accessToken,
|
|
99
|
+
token: accessToken,
|
|
88
100
|
loading,
|
|
89
101
|
isAuthenticated: !!user,
|
|
102
|
+
isLoggedIn: !!user,
|
|
90
103
|
login,
|
|
91
104
|
logout,
|
|
92
105
|
setAuth,
|
|
106
|
+
authFetch,
|
|
93
107
|
clientId,
|
|
94
108
|
redirectUri,
|
|
95
109
|
saBaseUrl,
|