From 9f03a0e5cc55fbfe22aeb81b6a0aa36356aae792 Mon Sep 17 00:00:00 2001 From: tonyrewin Date: Sat, 17 Sep 2022 21:23:30 +0300 Subject: [PATCH] auth send link common mutation --- clean-git.sh | 9 --- src/components/Views/Search.tsx | 4 +- src/components/Views/publicGraphQLClient.ts | 23 ------ src/graphql/index.ts | 2 +- .../{auth-forget.ts => auth-confirm-email.ts} | 4 +- src/graphql/mutation/auth-resend.ts | 9 --- .../{auth-reset.ts => auth-send-link.ts} | 4 +- src/graphql/publicGraphQLClient.ts | 4 +- src/graphql/query/article-reactions.ts | 2 +- .../{auth-check.ts => auth-check-email.ts} | 0 src/layouts/zine.astro | 8 +- src/stores/auth.ts | 20 ++--- src/stores/zine/topics.ts | 2 - src/utils/apiClient.ts | 79 +++++++++---------- ssr/server.mjs | 8 +- 15 files changed, 61 insertions(+), 117 deletions(-) delete mode 100755 clean-git.sh delete mode 100644 src/components/Views/publicGraphQLClient.ts rename src/graphql/mutation/{auth-forget.ts => auth-confirm-email.ts} (50%) delete mode 100644 src/graphql/mutation/auth-resend.ts rename src/graphql/mutation/{auth-reset.ts => auth-send-link.ts} (52%) rename src/graphql/query/{auth-check.ts => auth-check-email.ts} (100%) diff --git a/clean-git.sh b/clean-git.sh deleted file mode 100755 index 5ec6d1c7..00000000 --- a/clean-git.sh +++ /dev/null @@ -1,9 +0,0 @@ -git filter-branch --tag-name-filter 'cat' -f --tree-filter ' - find . -type d -name binarydir | while read dir - do - find $dir -type f -name "*.ear" -o -name "*.war" -o -name "*.jar" -o -name "*.zip" -o -name "*.exe" | while read file - do - git rm -r -f --ignore-unmatch $file - done - done -' -- --all diff --git a/src/components/Views/Search.tsx b/src/components/Views/Search.tsx index 5f0b7fa1..cf3b2d83 100644 --- a/src/components/Views/Search.tsx +++ b/src/components/Views/Search.tsx @@ -8,8 +8,8 @@ import { useArticlesStore, loadSearchResults } from '../../stores/zine/articles' import { useStore } from '@nanostores/solid' type Props = { - query: string - results: Shout[] + query?: string + results?: Shout[] } export const SearchPage = (props: Props) => { diff --git a/src/components/Views/publicGraphQLClient.ts b/src/components/Views/publicGraphQLClient.ts deleted file mode 100644 index 4e88dc8d..00000000 --- a/src/components/Views/publicGraphQLClient.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { ClientOptions, dedupExchange, fetchExchange, createClient, Exchange } from '@urql/core' -import { devtoolsExchange } from '@urql/devtools' - -// FIXME actual value -const isDev = true - -// export const baseUrl = 'https://.discours.io' -export const baseUrl = 'http://localhost:8000' - -const exchanges: Exchange[] = [dedupExchange, fetchExchange] - -if (isDev) { - exchanges.unshift(devtoolsExchange) -} - -const options: ClientOptions = { - url: baseUrl, - maskTypename: true, - requestPolicy: 'cache-and-network', - exchanges -} - -export const publicGraphQLClient = createClient(options) diff --git a/src/graphql/index.ts b/src/graphql/index.ts index a8366f9e..a102904d 100644 --- a/src/graphql/index.ts +++ b/src/graphql/index.ts @@ -2,7 +2,7 @@ import signIn from './query/auth-login' import signUp from './mutation/auth-register' import signOut from './mutation/auth-logout' -import checkEmail from './query/auth-check' +import checkEmail from './query/auth-check-email' import getSession from './mutation/my-session' // articles import topOverall from './query/articles-top-rated' diff --git a/src/graphql/mutation/auth-forget.ts b/src/graphql/mutation/auth-confirm-email.ts similarity index 50% rename from src/graphql/mutation/auth-forget.ts rename to src/graphql/mutation/auth-confirm-email.ts index 71450fa7..25165b1c 100644 --- a/src/graphql/mutation/auth-forget.ts +++ b/src/graphql/mutation/auth-confirm-email.ts @@ -1,8 +1,8 @@ import { gql } from '@urql/core' export default gql` - query ForgetQuery($email: String!) { - forget(email: $email) { + query ConfirmEmailQuery($code: String!) { + confirmEmail(code: $code) { error } } diff --git a/src/graphql/mutation/auth-resend.ts b/src/graphql/mutation/auth-resend.ts deleted file mode 100644 index d7b0e510..00000000 --- a/src/graphql/mutation/auth-resend.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { gql } from '@urql/core' - -export default gql` - query ResendQuery($email: String!) { - resend(email: $email) { - error - } - } -` diff --git a/src/graphql/mutation/auth-reset.ts b/src/graphql/mutation/auth-send-link.ts similarity index 52% rename from src/graphql/mutation/auth-reset.ts rename to src/graphql/mutation/auth-send-link.ts index 06bb2723..66df4214 100644 --- a/src/graphql/mutation/auth-reset.ts +++ b/src/graphql/mutation/auth-send-link.ts @@ -1,8 +1,8 @@ import { gql } from '@urql/core' export default gql` - query ResetQuery($code: String!) { - reset(code: $code) { + query SendLinkQuery($email: String!) { + sendLink(email: $email) { error } } diff --git a/src/graphql/publicGraphQLClient.ts b/src/graphql/publicGraphQLClient.ts index f6e84d32..1015211c 100644 --- a/src/graphql/publicGraphQLClient.ts +++ b/src/graphql/publicGraphQLClient.ts @@ -4,8 +4,8 @@ import { devtoolsExchange } from '@urql/devtools' // FIXME actual value const isDev = true -export const baseUrl = 'https://newapi.discours.io' -//export const baseUrl = 'http://localhost:8000' +//export const baseUrl = 'https://newapi.discours.io' +export const baseUrl = 'http://localhost:8000' const exchanges: Exchange[] = [dedupExchange, fetchExchange] diff --git a/src/graphql/query/article-reactions.ts b/src/graphql/query/article-reactions.ts index 8d6baa19..2ddc9602 100644 --- a/src/graphql/query/article-reactions.ts +++ b/src/graphql/query/article-reactions.ts @@ -1,7 +1,7 @@ import { gql } from '@urql/core' export default gql` - query ReactionsByShoutQuery($limit: String!, $limit: Int!, $offset: Int!) { + query ReactionsByShoutQuery($slug: String!, $limit: String!, $limit: Int!, $offset: Int!) { reactionsByShout(slug: $slug, limit: $limit, offset: $offset) { id body diff --git a/src/graphql/query/auth-check.ts b/src/graphql/query/auth-check-email.ts similarity index 100% rename from src/graphql/query/auth-check.ts rename to src/graphql/query/auth-check-email.ts diff --git a/src/layouts/zine.astro b/src/layouts/zine.astro index 1d3666e7..c1041193 100644 --- a/src/layouts/zine.astro +++ b/src/layouts/zine.astro @@ -5,17 +5,13 @@ import { Header } from '../components/Nav/Header' import { Footer } from '../components/Discours/Footer' import { ServerRouterProvider } from '../components/providers/ServerRouterProvider' import { t } from '../utils/intl' -import { locale as langstore } from '../stores/ui' -import { useStore } from '@nanostores/solid' const { pathname, search } = Astro.url - -// FIXME always returns ru -const locale = useStore(langstore) +const lang = Astro.url.searchParams['lang'] --- - + diff --git a/src/stores/auth.ts b/src/stores/auth.ts index f8061ef8..95d9e68b 100644 --- a/src/stores/auth.ts +++ b/src/stores/auth.ts @@ -1,4 +1,4 @@ -import { atom, action } from 'nanostores' +import { atom } from 'nanostores' import type { AuthResult } from '../graphql/types.gen' import { getLogger } from '../utils/logger' import { resetToken, setToken } from '../graphql/privateGraphQLClient' @@ -9,14 +9,14 @@ const log = getLogger('auth-store') export const session = atom() export const signIn = async (params) => { - const s = await apiClient.signIn(params) + const s = await apiClient.authLogin(params) session.set(s) setToken(s.token) log.debug('signed in') } export const signUp = async (params) => { - const s = await apiClient.signUp(params) + const s = await apiClient.authRegiser(params) session.set(s) setToken(s.token) log.debug('signed up') @@ -31,22 +31,18 @@ export const signOut = () => { export const emailChecks = atom<{ [email: string]: boolean }>({}) export const signCheck = async (params) => { - emailChecks.set(await apiClient.signCheck(params)) + emailChecks.set(await apiClient.authCheckEmail(params)) } export const resetCode = atom() -export const signReset = async (params) => { - await apiClient.signReset(params) // { email } +export const signSendLink = async (params) => { + await apiClient.authSendLink(params) // { email } resetToken() } -export const signResend = async (params) => { - await apiClient.signResend(params) // { email } -} - -export const signResetConfirm = async (params) => { - const auth = await apiClient.signResetConfirm(params) // { code } +export const signConfirm = async (params) => { + const auth = await apiClient.authConfirmCode(params) // { code } setToken(auth.token) session.set(auth) } diff --git a/src/stores/zine/topics.ts b/src/stores/zine/topics.ts index 182165c3..9788f30f 100644 --- a/src/stores/zine/topics.ts +++ b/src/stores/zine/topics.ts @@ -105,7 +105,6 @@ type InitialState = { } export const useTopicsStore = ({ topics, randomTopics }: InitialState = {}) => { - // console.log('using topics store') if (topics) { addTopics(topics) } @@ -114,7 +113,6 @@ export const useTopicsStore = ({ topics, randomTopics }: InitialState = {}) => { } if (!randomTopicsStore) { randomTopicsStore = atom(randomTopics) - // console.log('random topics separate store') } const getTopicEntities = useStore(topicEntitiesStore) diff --git a/src/utils/apiClient.ts b/src/utils/apiClient.ts index 2663ad6a..04ef1418 100644 --- a/src/utils/apiClient.ts +++ b/src/utils/apiClient.ts @@ -10,9 +10,12 @@ import { getLogger } from './logger' import reactionsForShouts from '../graphql/query/reactions-for-shouts' import mySession from '../graphql/mutation/my-session' import { privateGraphQLClient } from '../graphql/privateGraphQLClient' -import authLogout from '../graphql/mutation/auth-logout' -import authLogin from '../graphql/query/auth-login' -import authRegister from '../graphql/mutation/auth-register' +import authLogoutQuery from '../graphql/mutation/auth-logout' +import authLoginQuery from '../graphql/query/auth-login' +import authRegisterMutation from '../graphql/mutation/auth-register' +import authCheckEmailQuery from '../graphql/query/auth-check-email' +import authConfirmCodeMutation from '../graphql/mutation/auth-confirm-email' +import authSendLinkMutation from '../graphql/mutation/auth-send-link' import followMutation from '../graphql/mutation/follow' import unfollowMutation from '../graphql/mutation/unfollow' import articlesForAuthors from '../graphql/query/articles-for-authors' @@ -25,10 +28,6 @@ import authorsAll from '../graphql/query/authors-all' import reactionCreate from '../graphql/mutation/reaction-create' import reactionDestroy from '../graphql/mutation/reaction-destroy' import reactionUpdate from '../graphql/mutation/reaction-update' -import authCheck from '../graphql/query/auth-check' -import authReset from '../graphql/mutation/auth-reset' -import authForget from '../graphql/mutation/auth-forget' -import authResend from '../graphql/mutation/auth-resend' import authorsBySlugs from '../graphql/query/authors-by-slugs' import incrementView from '../graphql/mutation/increment-view' @@ -39,6 +38,36 @@ const REACTIONS_PAGE_SIZE = 100 const DEFAULT_RANDOM_TOPICS_AMOUNT = 12 export const apiClient = { + // auth + + authLogin: async ({ email, password }) => { + const response = await publicGraphQLClient.query(authLoginQuery, { email, password }).toPromise() + return response.data.signIn + }, + authRegiser: async ({ email, password }) => { + const response = await publicGraphQLClient.query(authRegisterMutation, { email, password }).toPromise() + return response.data.registerUser + }, + authSignOut: async () => { + const response = await publicGraphQLClient.query(authLogoutQuery, {}).toPromise() + return response.data.signOut + }, + authCheckEmail: async ({ email }) => { + // check if email is used + const response = await publicGraphQLClient.query(authCheckEmailQuery, { email }).toPromise() + return response.data.isEmailUsed + }, + authSendLink: async ({ email }) => { + // send link with code on email + const response = await publicGraphQLClient.query(authSendLinkMutation, { email }).toPromise() + return response.data.reset + }, + authConfirmCode: async ({ code }) => { + // confirm email with code from link + const response = await publicGraphQLClient.query(authConfirmCodeMutation, { code }).toPromise() + return response.data.reset + }, + getTopArticles: async () => { const response = await publicGraphQLClient.query(articlesTopRated, { limit: 10, offset: 0 }).toPromise() return response.data.topOverall @@ -150,40 +179,6 @@ export const apiClient = { return response.data.unfollow }, - // auth - - signIn: async ({ email, password }) => { - const response = await publicGraphQLClient.query(authLogin, { email, password }).toPromise() - return response.data.signIn - }, - signUp: async ({ email, password }) => { - const response = await publicGraphQLClient.query(authRegister, { email, password }).toPromise() - return response.data.registerUser - }, - signOut: async () => { - const response = await publicGraphQLClient.query(authLogout, {}).toPromise() - return response.data.signOut - }, - signCheck: async ({ email }) => { - // check if email is used - const response = await publicGraphQLClient.query(authCheck, { email }).toPromise() - return response.data.isEmailUsed - }, - signReset: async ({ email }) => { - // send reset link with code on email - const response = await publicGraphQLClient.query(authForget, { email }).toPromise() - return response.data.reset - }, - signResend: async ({ email }) => { - // same as reset if code is expired - const response = await publicGraphQLClient.query(authResend, { email }).toPromise() - return response.data.resend - }, - signResetConfirm: async ({ code }) => { - // confirm reset password with code - const response = await publicGraphQLClient.query(authReset, { code }).toPromise() - return response.data.reset - }, getSession: async () => { // renew session with auth token in header (!) const response = await privateGraphQLClient.mutation(mySession, {}).toPromise() @@ -247,7 +242,7 @@ export const apiClient = { }) .toPromise() - return response.data.reactionsByShout + return response.data?.reactionsByShout }, getAuthorsBySlugs: async ({ slugs }) => { const response = await publicGraphQLClient.query(authorsBySlugs, { slugs }).toPromise() diff --git a/ssr/server.mjs b/ssr/server.mjs index 67605f1a..1ba10a77 100644 --- a/ssr/server.mjs +++ b/ssr/server.mjs @@ -30,12 +30,12 @@ async function handle(req, res) { } const server = createServer((req, res) => { - handle(req, res).catch((err) => { - console.error('[ssr] server error', err); + handle(req, res).catch((error) => { + console.error('[ssr] server error', error); res.writeHead(500, { 'Content-Type': 'text/plain', }); - res.end(err.toString()); + res.end(error.toString()); }); }); @@ -43,4 +43,4 @@ server.listen(8085); console.log('[ssr] serving at http://localhost:8085'); // Silence weird