Skip to content

Commit

Permalink
refactor: magic link auth
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosoul committed Jun 20, 2022
1 parent b25907f commit b938b6b
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 111 deletions.
37 changes: 29 additions & 8 deletions src/app/services/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ export const authApi = createApi({
}
}
}),
register: builder.mutation({
query: (data) => ({
url: `user/register`,
method: "POST",
body: data
})
}),
// 更新token
renew: builder.mutation({
query: ({ token, refreshToken }) => ({
Expand Down Expand Up @@ -87,9 +94,9 @@ export const authApi = createApi({
})
}),

checkInviteTokenValid: builder.mutation({
checkMagicTokenValid: builder.mutation({
query: (token) => ({
url: "user/check_invite_magic_token",
url: "user/check_magic_token",
method: "POST",
body: { magic_token: token }
})
Expand All @@ -101,11 +108,23 @@ export const authApi = createApi({
body: { old_password, new_password }
})
}),
sendMagicLink: builder.mutation({
sendLoginMagicLink: builder.mutation({
query: (email) => ({
url: "token/send_magic_link",
headers: {
// "content-type": "text/plain",
accept: "text/plain"
},
url: `user/send_login_magic_link?email=${encodeURIComponent(email)}`,
method: "POST",
responseHandler: (response) => response.text()
// body: { email }
})
}),
sendRegMagicLink: builder.mutation({
query: (data) => ({
url: `user/send_reg_magic_link`,
method: "POST",
body: { email }
body: data
})
}),
getMetamaskNonce: builder.query({
Expand Down Expand Up @@ -147,14 +166,16 @@ export const authApi = createApi({

export const {
useGetInitializedQuery,
useSendMagicLinkMutation,
useSendLoginMagicLinkMutation,
useSendRegMagicLinkMutation,
useGetCredentialsQuery,
useUpdateDeviceTokenMutation,
useGetOpenidMutation,
useRenewMutation,
useLazyGetMetamaskNonceQuery,
useLoginMutation,
useLazyLogoutQuery,
useCheckInviteTokenValidMutation,
useUpdatePasswordMutation
useCheckMagicTokenValidMutation,
useUpdatePasswordMutation,
useRegisterMutation
} = authApi;
5 changes: 3 additions & 2 deletions src/app/services/base.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import BASE_URL, { tokenHeader } from "../config";
const whiteList = [
"login",
"register",
"sendMagicLink",
"checkInviteTokenValid",
"sendLoginMagicLink",
"sendRegMagicLink",
"checkMagicTokenValid",
"getGoogleAuthConfig",
"getGithubAuthConfig",
"getSMTPStatus",
Expand Down
11 changes: 2 additions & 9 deletions src/app/services/contact.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,7 @@ export const contactApi = createApi({
body: data
})
}),
register: builder.mutation({
query: (data) => ({
url: `user/register`,
method: "POST",
body: data
})
}),

sendMsg: builder.mutation({
query: ({ id, content, type = "text", properties = "" }) => ({
headers: {
Expand Down Expand Up @@ -139,6 +133,5 @@ export const {
useUpdateAvatarMutation,
useGetContactsQuery,
useLazyGetContactsQuery,
useSendMsgMutation,
useRegisterMutation
useSendMsgMutation
} = contactApi;
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
// import { useState, useEffect } from "react";
// import { useGoogleLogin } from "react-google-login";
import IconGithub from "../../assets/icons/github.svg";
import { StyledSocialButton } from "./styled";
import styled from "styled-components";
import Button from "./styled/Button";
const StyledSocialButton = styled(Button)`
width: 100%;
margin-bottom: 16px;
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
color: #344054;
border: 1px solid #d0d5dd;
background: none !important;
.icon {
width: 24px;
height: 24px;
}
`;
export default function GithubLoginButton({ config = {} }) {
const { client_id } = config;
const handleGithubLogin = () => {
Expand Down
58 changes: 58 additions & 0 deletions src/common/component/GoogleLoginButton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useEffect } from "react";
import { useGoogleLogin } from "react-google-login";

import googleSvg from "../../assets/icons/google.svg?url";
import styled from "styled-components";
import Button from "./styled/Button";
import { useLoginMutation } from "../../app/services/auth";
import toast from "react-hot-toast";
const StyledSocialButton = styled(Button)`
width: 100%;
margin-bottom: 16px;
display: flex;
align-items: center;
justify-content: center;
gap: 12px;
color: #344054;
border: 1px solid #d0d5dd;
background: none !important;
.icon {
width: 24px;
height: 24px;
}
`;
export default function GoogleLoginButton({ clientId }) {
const [login, { isSuccess, isLoading }] = useLoginMutation();
const { signIn, loaded } = useGoogleLogin({
onScriptLoadFailure: (wtf) => {
console.error("google login script load failure", wtf);
},
clientId,
onSuccess: ({ tokenId, ...rest }) => {
console.info("google oauth success", tokenId, rest);
login({
id_token: tokenId,
type: "google"
});
},
onFailure: (wtf) => {
console.error("google login failure", wtf);
}
});
useEffect(() => {
if (isSuccess) {
toast.success("Login Successfully");
// navigateTo("/");
}
}, [isSuccess]);
const handleGoogleLogin = () => {
signIn();
};
// console.log("google login ", loaded);
return (
<StyledSocialButton disabled={!loaded || isLoading} onClick={handleGoogleLogin}>
<img className="icon" src={googleSvg} alt="google icon" />
{loaded ? `Sign in with Google` : `Initailizing`}
</StyledSocialButton>
);
}
4 changes: 2 additions & 2 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ const PageRoutes = () => {
}
>
<Route index element={<RegPage />} />
<Route path="magiclink">
<Route path="set_name">
<Route index element={<RegWithUsernamePage />} />
<Route path=":token" element={<RegWithUsernamePage />} />
<Route path=":from" element={<RegWithUsernamePage />} />
</Route>
</Route>
<Route
Expand Down
6 changes: 3 additions & 3 deletions src/routes/invite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import StyledWrapper from "./styled";
import { Navigate } from "react-router-dom";
import toast from "react-hot-toast";
import BASE_URL from "../../app/config";
import { useRegisterMutation } from "../../app/services/contact";
import { useCheckInviteTokenValidMutation } from "../../app/services/auth";
import { useRegisterMutation } from "../../app/services/auth";
import { useCheckMagicTokenValidMutation } from "../../app/services/auth";
import { useSelector } from "react-redux";

export default function InvitePage() {
Expand All @@ -17,7 +17,7 @@ export default function InvitePage() {
// const navigateTo = useNavigate();
const [register, { data, isLoading, isSuccess, isError, error }] = useRegisterMutation();
const [checkToken, { data: isValid, isLoading: checkLoading, isSuccess: checkSuccess }] =
useCheckInviteTokenValidMutation();
useCheckMagicTokenValidMutation();
useEffect(() => {
// console.log(search);
const query = new URLSearchParams(location.search);
Expand Down
33 changes: 0 additions & 33 deletions src/routes/login/GoogleLoginButton.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/routes/login/SignUpLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const StyledSignUpLink = styled.p`
export default function MagicLinkLogin() {
const navigate = useNavigate();
const handleSignUp = () => {
navigate("/");
navigate("/register");
};
return (
<StyledSignUpLink>
Expand Down
18 changes: 9 additions & 9 deletions src/routes/login/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import MetamaskLoginButton from "./MetamaskLoginButton";
import OidcLoginButton from "./OidcLoginButton";
import Input from "../../common/component/styled/Input";
import Button from "../../common/component/styled/Button";
import GoogleLoginButton from "./GoogleLoginButton";
import MagicLinkLogin from "./MagicLinkLogin";
import SignUpLink from "./SignUpLink";
import { useLoginMutation } from "../../app/services/auth";
import { useGetLoginConfigQuery, useGetSMTPStatusQuery } from "../../app/services/server";
import useGoogleAuthConfig from "../../common/hook/useGoogleAuthConfig";
import GithubLoginButton from "./GithubLoginButton";
import useGithubAuthConfig from "../../common/hook/useGithubAuthConfig";
import GoogleLoginButton from "../../common/component/GoogleLoginButton";
import GithubLoginButton from "../../common/component/GithubLoginButton";
export default function LoginPage() {
const { data: enableSMTP } = useGetSMTPStatusQuery();
const [login, { isSuccess, isLoading, error }] = useLoginMutation();
Expand All @@ -32,7 +32,7 @@ export default function LoginPage() {
const oauth = query.get("oauth");
const code = query.get("code");
const state = query.get("state");
const token = query.get("token");
const magic_token = query.get("magic_token");
const exists = query.get("exists");
if (oauth) {
switch (oauth) {
Expand All @@ -58,18 +58,18 @@ export default function LoginPage() {
}
}
// magic link
if (token && typeof exists !== "undefined") {
console.log("tokken", token, exists);
if (magic_token && typeof exists !== "undefined") {
// console.log("tokken", token, exists);
const isLogin = exists == "true";
if (isLogin) {
// login
login({
token,
magic_token,
type: "magiclink"
});
} else {
// reg
location.href = `/#/reg/magiclink/${token}`;
// reg with magic link and set name only
location.href = `?magic_token=${magic_token}#/register/set_name/login`;
}
}
}, []);
Expand Down Expand Up @@ -167,7 +167,7 @@ export default function LoginPage() {
</form>
{hasDivider && <hr className="or" />}
{enableMagicLink && <MagicLinkLogin />}
{googleLogin && <GoogleLoginButton login={login} clientId={clientId} />}
{googleLogin && <GoogleLoginButton clientId={clientId} />}
{enableGithubLogin && <GithubLoginButton config={githubAuthConfig} />}
{enableMetamaskLogin && <MetamaskLoginButton login={login} />}
{oidc.length > 0 && <OidcLoginButton issuers={oidc} />}
Expand Down
33 changes: 33 additions & 0 deletions src/routes/reg/EmailNextStepTip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// import React from "react";
import styled from "styled-components";
const Styled = styled.div`
display: flex;
flex-direction: column;
align-items: center;
.title {
font-weight: 600;
font-size: 30px;
line-height: 38px;
color: #101828;
margin-bottom: 12px;
}
.desc {
text-align: center;
font-weight: 400;
font-size: 16px;
line-height: 24px;
color: #667085;
&:not(:last-child) {
margin-bottom: 24px;
}
}
`;
export default function EmailNextTip() {
return (
<Styled>
<div className="title">Magic link Sent</div>
<p className="desc">Login to your email client, and continue next step</p>
<p className="desc">You can close this window now.</p>
</Styled>
);
}
Loading

0 comments on commit b938b6b

Please sign in to comment.