@canmingir/link 1.2.2 → 1.2.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canmingir/link",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./index.js",
@@ -1,3 +1,5 @@
1
+ import { Route, Routes } from "react-router-dom";
2
+
1
3
  import Callback from "../pages/Callback";
2
4
  import CompactLayout from "../layouts/CompactLayout";
3
5
  import ConfigError from "../pages/ConfigError";
@@ -9,8 +11,6 @@ import classicLoginLayout from "../layouts/auth/classic";
9
11
  import config from "../config/config";
10
12
  import modernLoginLayout from "../layouts/auth/modern";
11
13
 
12
- import { Route, Routes } from "react-router-dom";
13
-
14
14
  export default function RouteManager({ routes }) {
15
15
  const loginConfig = config().template?.login;
16
16
 
@@ -30,7 +30,6 @@ export default function RouteManager({ routes }) {
30
30
  >
31
31
  <Route index element={<LoginPage />} />
32
32
  </Route>
33
- <Route path="/callback/:provider" element={<Callback />} />
34
33
  <Route path="/callback" element={<Callback />} />
35
34
  </>
36
35
  )}
@@ -1,3 +1,5 @@
1
+ import { useEffect, useRef } from "react";
2
+
1
3
  import Page from "../layouts/Page";
2
4
  import React from "react";
3
5
  import config from "../config/config";
@@ -6,9 +8,7 @@ import qs from "qs";
6
8
  import { storage } from "@nucleoidjs/webstorage";
7
9
  import { useContext } from "../ContextProvider/ContextProvider";
8
10
  import { useLocation } from "react-router-dom";
9
-
10
- import { useEffect, useRef } from "react";
11
- import { useNavigate, useParams } from "react-router-dom";
11
+ import { useNavigate } from "react-router-dom";
12
12
 
13
13
  function Callback() {
14
14
  const { project: appConfig, name, appId } = config();
@@ -18,8 +18,6 @@ function Callback() {
18
18
  const [, dispatch] = useContext();
19
19
  const location = useLocation();
20
20
  const navigate = useNavigate();
21
- const { provider } = useParams();
22
-
23
21
  const hasProcessed = useRef(false);
24
22
 
25
23
  useEffect(() => {
@@ -30,6 +28,14 @@ function Callback() {
30
28
  const parsedQuery = qs.parse(location.search, { ignoreQueryPrefix: true });
31
29
  const { code, error, error_description, state } = parsedQuery;
32
30
 
31
+ let provider;
32
+ let stateData = {};
33
+
34
+ if (state) {
35
+ stateData = JSON.parse(decodeURIComponent(state));
36
+ provider = stateData.provider;
37
+ }
38
+
33
39
  if (error) {
34
40
  console.error("OAuth error:", error, error_description);
35
41
  navigate(
@@ -89,7 +95,6 @@ function Callback() {
89
95
 
90
96
  storage.set(name, "accessToken", accessToken);
91
97
  storage.set(name, "refreshToken", refreshToken);
92
- storage.set(name, "provider", provider);
93
98
 
94
99
  dispatch({ type: "LOGIN", payload: { user: userInfo } });
95
100
 
@@ -1,13 +1,20 @@
1
+ import { Box, Divider, Link as MuiLink, Typography } from "@mui/material";
2
+ import React, { useState } from "react";
3
+
1
4
  import NucleoidLoginForm from "../../components/NucleoidLoginForm";
2
5
  import SocialLoginButtons from "../../components/SocialLoginButtons";
3
6
  import Stack from "@mui/material/Stack";
4
7
  import config from "../../config/config";
5
8
 
6
- import { Box, Divider, Link as MuiLink, Typography } from "@mui/material";
7
- import React, { useState } from "react";
8
-
9
- const handleOAuthLogin = ({ redirectUri, authUrl, clientId, scope }) => {
10
- window.location.href = `${authUrl}?client_id=${clientId}&scope=${scope}&response_type=code&redirect_uri=${redirectUri}`;
9
+ const handleOAuthLogin = (
10
+ { redirectUri, authUrl, clientId, scope },
11
+ provider
12
+ ) => {
13
+ const state = JSON.stringify({
14
+ provider: provider,
15
+ });
16
+ const encodedState = encodeURIComponent(state);
17
+ window.location.href = `${authUrl}?client_id=${clientId}&scope=${scope}&response_type=code&redirect_uri=${redirectUri}&state=${encodedState}`;
11
18
  };
12
19
 
13
20
  function LoginForm() {
@@ -62,11 +69,11 @@ function LoginForm() {
62
69
  )}
63
70
  <SocialLoginButtons
64
71
  googleEnable={!!project.google}
65
- onGoogle={() => handleOAuthLogin({ ...project.google })}
72
+ onGoogle={() => handleOAuthLogin({ ...project.google }, "google")}
66
73
  githubEnable={!!project.github}
67
- onGithub={() => handleOAuthLogin({ ...project.github })}
74
+ onGithub={() => handleOAuthLogin({ ...project.github }, "github")}
68
75
  linkedinEnable={!!project.linkedin}
69
- onLinkedin={() => handleOAuthLogin({ ...project.linkedin })}
76
+ onLinkedin={() => handleOAuthLogin({ ...project.linkedin }, "linkedin")}
70
77
  />
71
78
  </>
72
79
  );