diff --git a/bundle.js b/bundle.js index fdb57a0..bcb27dd 100644 --- a/bundle.js +++ b/bundle.js @@ -1 +1 @@ -"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("crypto-js");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=t(e);const a=process.env.KINDE_REDIRECT_URL,i=process.env.KINDE_ISSUER_URL,o=process.env.KINDE_POST_LOGOUT_REDIRECT_ROUTE,c={initialState:{user:null,isAuthenticated:!1,isLoading:!0,checkSession:null},SESSION_PREFIX:"pkce-verifier",redirectURL:a,issuerURL:i,clientID:process.env.KINDE_CLIENT_ID,clientSecret:process.env.KINDE_CLIENT_SECRET,responseType:"code",scope:"openid offline",codeChallengeMethod:"S256",redirectRoutes:{callback:"/api/auth/kinde_callback",postLogoutRedirect:o||"/"},issuerRoutes:{logout:"/logout",login:"/oauth2/auth",register:"/oauth2/auth",token:"/oauth2/token",profile:"/oauth2/user_profile"}},n=()=>{throw new Error("Oops! Seems like you forgot to wrap your app in .")},u=e.createContext({...c.initialState,user:n,isLoading:n,isAuthenticated:n,checkSession:n});const d=require("crypto"),l=()=>d.randomBytes(28).toString("hex");function h(){const e=l(),t=function(e){return r.SHA256(e).toString(r.enc.Base64url)}(e);return{code_verifier:e,code_challenge:t}}var p=require("cookie");const _=(e,r,t)=>{const s=l(),{code_challenge:a,code_verifier:i}=h();return r.setHeader("Set-Cookie",p.serialize(`${c.SESSION_PREFIX}-${s}`,i,{httpOnly:!0,maxAge:t})),{state:s,code_challenge:a}};var R=require("cookie");var g=require("cookie");var y=require("cookie");exports.AuthContext=u,exports.KindeProvider=({children:r,initialUser:t})=>{const[a,i]=e.useState({...c.initialState,user:t,isLoading:!t,isAuthenticated:!!t}),o="/api/auth/me",n=e.useCallback((async()=>{try{const e=await(async e=>{let r;try{r=await fetch(e)}catch{throw new RequestError(0)}if(r.ok)return r.json();r.status})(o);i((r=>({...r,user:e,error:void 0})))}catch(e){i((r=>({...r,error:e})))}}),[o]);e.useEffect((()=>{a.user||(async()=>{await n(),i((e=>({...e,isLoading:!1,isAuthenticated:!!a.user})))})()}),[a.user]);const{user:d,error:l,isLoading:h,isAuthenticated:p}=a;return s.default.createElement(u.Provider,{value:{user:d,error:l,isLoading:h,isAuthenticated:p}},r)},exports.handleAuth=()=>async function(e,r){let{query:{kindeAuth:t}}=e;switch(t=Array.isArray(t)?t[0]:t,t){case"login":return await(async(e,r)=>{const{state:t,code_challenge:s}=_(0,r,60),a=new URL(c.issuerURL+c.issuerRoutes.login);a.searchParams.append("response_type",c.responseType),a.searchParams.append("client_id",c.clientID),a.searchParams.append("redirect_uri",c.redirectURL+c.redirectRoutes.callback),a.searchParams.append("scope",c.scope),a.searchParams.set("state",t),a.searchParams.set("code_challenge",s),a.searchParams.set("code_challenge_method",c.codeChallengeMethod),r.redirect(a.href)})(0,r);case"register":return await(async(e,r)=>{const{state:t,code_challenge:s}=_(0,r,180),a=new URL(c.issuerURL+c.issuerRoutes.register);a.searchParams.append("response_type",c.responseType),a.searchParams.append("client_id",c.clientID),a.searchParams.append("redirect_uri",c.redirectURL+c.redirectRoutes.callback),a.searchParams.append("scope",c.scope),a.searchParams.append("start_page","registration"),a.searchParams.set("state",t),a.searchParams.set("code_challenge",s),a.searchParams.set("code_challenge_method",c.codeChallengeMethod),r.redirect(a.href)})(0,r);case"me":return await(async(e,r)=>{const t=g.parse(e.headers.cookie||"").kinde_token;if(t){const e=JSON.parse(t);try{const t=await fetch(c.issuerURL+c.issuerRoutes.profile,{headers:new Headers({Authorization:"Bearer "+e.access_token})}),s=await t.json();r.send(s)}catch(e){console.log(e)}}else r.status(401).send("Unauthorized")})(e,r);case"logout":return await(async(e,r)=>{r.setHeader("Set-Cookie",R.serialize("kinde_token",null,{httpOnly:!0,maxAge:0}));const t=new URL(c.issuerURL+c.issuerRoutes.logout);t.searchParams.set("redirect",c.redirectURL+c.redirectRoutes.postLogoutRedirect),r.redirect(t.href)})(0,r);case"kinde_callback":return await(async(e,r)=>{const{code:t,state:s}=e.query,a=y.parse(e.headers.cookie||"")[`${c.SESSION_PREFIX}-${s}`];if(a){try{const e=await fetch(c.issuerURL+c.issuerRoutes.token,{method:"POST",headers:new Headers({"Content-type":"application/x-www-form-urlencoded; charset=UTF-8"}),body:new URLSearchParams({client_id:c.clientID,client_secret:c.clientSecret,code:t,code_verifier:a,grant_type:"authorization_code",redirect_uri:c.redirectURL+c.redirectRoutes.callback})}),s=await e.json();r.setHeader("Set-Cookie",y.serialize("kinde_token",JSON.stringify(s),{httpOnly:!0,maxAge:Number(s.expires_in)}))}catch(e){console.log(e)}r.redirect(c.redirectURL)}else r.redirect(c.redirectURL)})(e,r);default:return r.status(404).end()}},exports.useKindeAuth=()=>e.useContext(u); +"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("react"),r=require("crypto-js");function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=t(e);const a=process.env.KINDE_REDIRECT_URL,i=process.env.KINDE_ISSUER_URL,c=process.env.KINDE_POST_LOGOUT_REDIRECT_ROUTE,o={initialState:{user:null,isAuthenticated:!1,isLoading:!0,checkSession:null},SESSION_PREFIX:"pkce-verifier",redirectURL:a,issuerURL:i,clientID:process.env.KINDE_CLIENT_ID,clientSecret:process.env.KINDE_CLIENT_SECRET,responseType:"code",scope:"openid offline",codeChallengeMethod:"S256",redirectRoutes:{callback:"/api/auth/kinde_callback",postLogoutRedirect:c||"/"},issuerRoutes:{logout:"/logout",login:"/oauth2/auth",register:"/oauth2/auth",token:"/oauth2/token",profile:"/oauth2/user_profile"}},n=()=>{throw new Error("Oops! Seems like you forgot to wrap your app in .")},u=e.createContext({...o.initialState,user:n,isLoading:n,isAuthenticated:n,checkSession:n});const d=require("crypto"),l=()=>d.randomBytes(28).toString("hex");function h(){const e=l(),t=function(e){return r.SHA256(e).toString(r.enc.Base64url)}(e);return{code_verifier:e,code_challenge:t}}var p=require("cookie");const _=(e,r,t)=>{const s=l(),{code_challenge:a,code_verifier:i}=h();return r.setHeader("Set-Cookie",p.serialize(`${o.SESSION_PREFIX}-${s}`,i,{httpOnly:!0,maxAge:t})),{state:s,code_challenge:a}};var R=require("cookie");var g=require("cookie");var y=require("cookie");exports.AuthContext=u,exports.KindeProvider=({children:r,initialUser:t})=>{const[a,i]=e.useState({...o.initialState,user:t,isLoading:!t,isAuthenticated:!!t}),c="/api/auth/me",n=e.useCallback((async()=>{try{const e=await(async e=>{let r;try{r=await fetch(e)}catch{throw new RequestError(0)}if(r.ok)return r.json();r.status})(c);i((r=>({...r,user:e,isAuthenticated:!!a.user,error:void 0})))}catch(e){i((r=>({...r,error:e})))}}),[c]);e.useEffect((()=>{a.user||(async()=>{await n(),i((e=>({...e,isLoading:!1,isAuthenticated:!!a.user})))})()}),[a.user]);const{user:d,error:l,isLoading:h,isAuthenticated:p}=a;return s.default.createElement(u.Provider,{value:{user:d,error:l,isLoading:h,isAuthenticated:p}},r)},exports.handleAuth=()=>async function(e,r){let{query:{kindeAuth:t}}=e;switch(t=Array.isArray(t)?t[0]:t,t){case"login":return await(async(e,r)=>{const{state:t,code_challenge:s}=_(0,r,60),a=new URL(o.issuerURL+o.issuerRoutes.login);a.searchParams.append("response_type",o.responseType),a.searchParams.append("client_id",o.clientID),a.searchParams.append("redirect_uri",o.redirectURL+o.redirectRoutes.callback),a.searchParams.append("scope",o.scope),a.searchParams.set("state",t),a.searchParams.set("code_challenge",s),a.searchParams.set("code_challenge_method",o.codeChallengeMethod),r.redirect(a.href)})(0,r);case"register":return await(async(e,r)=>{const{state:t,code_challenge:s}=_(0,r,180),a=new URL(o.issuerURL+o.issuerRoutes.register);a.searchParams.append("response_type",o.responseType),a.searchParams.append("client_id",o.clientID),a.searchParams.append("redirect_uri",o.redirectURL+o.redirectRoutes.callback),a.searchParams.append("scope",o.scope),a.searchParams.append("start_page","registration"),a.searchParams.set("state",t),a.searchParams.set("code_challenge",s),a.searchParams.set("code_challenge_method",o.codeChallengeMethod),r.redirect(a.href)})(0,r);case"me":return await(async(e,r)=>{const t=g.parse(e.headers.cookie||"").kinde_token;if(t){const e=JSON.parse(t);try{const t=await fetch(o.issuerURL+o.issuerRoutes.profile,{headers:new Headers({Authorization:"Bearer "+e.access_token})}),s=await t.json();r.send(s)}catch(e){console.log(e)}}else r.status(401).send("Unauthorized")})(e,r);case"logout":return await(async(e,r)=>{r.setHeader("Set-Cookie",R.serialize("kinde_token",null,{httpOnly:!0,maxAge:0}));const t=new URL(o.issuerURL+o.issuerRoutes.logout);t.searchParams.set("redirect",o.redirectURL+o.redirectRoutes.postLogoutRedirect),r.redirect(t.href)})(0,r);case"kinde_callback":return await(async(e,r)=>{const{code:t,state:s}=e.query,a=y.parse(e.headers.cookie||"")[`${o.SESSION_PREFIX}-${s}`];if(a){try{const e=await fetch(o.issuerURL+o.issuerRoutes.token,{method:"POST",headers:new Headers({"Content-type":"application/x-www-form-urlencoded; charset=UTF-8"}),body:new URLSearchParams({client_id:o.clientID,client_secret:o.clientSecret,code:t,code_verifier:a,grant_type:"authorization_code",redirect_uri:o.redirectURL+o.redirectRoutes.callback})}),s=await e.json();r.setHeader("Set-Cookie",y.serialize("kinde_token",JSON.stringify(s),{httpOnly:!0,maxAge:Number(s.expires_in)}))}catch(e){console.log(e)}r.redirect(o.redirectURL)}else r.redirect(o.redirectURL)})(e,r);default:return r.status(404).end()}},exports.useKindeAuth=()=>e.useContext(u); diff --git a/src/frontend/AuthProvider.jsx b/src/frontend/AuthProvider.jsx index b1cc093..5f00051 100644 --- a/src/frontend/AuthProvider.jsx +++ b/src/frontend/AuthProvider.jsx @@ -55,6 +55,7 @@ export default ({ children, initialUser }) => { setState((previous) => ({ ...previous, user, + isAuthenticated: !!state.user, error: undefined, })); } catch (error) {