Skip to content

Commit

Permalink
fix: Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
typeofweb committed Oct 5, 2021
1 parent 31d0ee2 commit a6cec67
Show file tree
Hide file tree
Showing 7 changed files with 1,123 additions and 649 deletions.
1 change: 0 additions & 1 deletion .env-sample
Expand Up @@ -27,7 +27,6 @@ GITHUB_ID="supertajne"
GITHUB_SECRET="bardzotajne"
SECRET="anothersecret"
JWT_SECRET="hmmmmmm"
NEXTAUTH_URL="http://localhost:3000"

NEXT_PUBLIC_ALGOLIA_API_KEY=""
NEXT_PUBLIC_ALGOLIA_APP_ID=""
Expand Down
8 changes: 2 additions & 6 deletions components/AdminPanel/AdminPanel.tsx
Expand Up @@ -42,11 +42,7 @@ export const AdminPanel = () => {
[changeQuery],
);

if (!blogs) {
return <p>Ładowanie...</p>;
}

const tableData = blogs.map((blog) => ({
const tableData = blogs?.map((blog) => ({
...blog,
isPublic: blog.isPublic ? '✅' : '❌',
lastArticlePublishedAt: blog.lastArticlePublishedAt
Expand Down Expand Up @@ -81,7 +77,7 @@ export const AdminPanel = () => {
<option value="false">Tylko ukryte</option>
</select>
</label>
<Table columns={columns} data={tableData} />
{tableData && <Table columns={columns} data={tableData} />}
</section>
</AuthGuard>
);
Expand Down
3 changes: 3 additions & 0 deletions next-env.d.ts
@@ -1,3 +1,6 @@
/// <reference types="next" />
/// <reference types="next/types/global" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
114 changes: 60 additions & 54 deletions package.json
Expand Up @@ -17,7 +17,7 @@
"db:migrate:up": "prisma migrate deploy --preview-feature && prisma generate",
"build": "next build",
"start": "next start",
"eslint": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
"eslint": "eslint --resolve-plugins-relative-to . --ext .js,.jsx,.ts,.tsx --fix .",
"test": "jest --detectOpenHandles --forceExit --passWithNoTests",
"test:ci": "jest --passWithNoTests",
"tsc": "tsc --noEmit",
Expand All @@ -34,105 +34,105 @@
"license": "AGPL-3.0-only",
"private": true,
"dependencies": {
"@hapi/boom": "9.1.3",
"@hcaptcha/react-hcaptcha": "0.3.6",
"@prisma/client": "2.26.0",
"@sentry/browser": "6.8.0",
"@sentry/integrations": "6.8.0",
"@sentry/node": "6.8.0",
"@sentry/react": "6.8.0",
"@sentry/tracing": "6.8.0",
"@sentry/webpack-plugin": "1.16.0",
"@supabase/supabase-js": "1.18.1",
"@supabase/ui": "0.28.1",
"algoliasearch": "4.10.3",
"@hapi/boom": "9.1.4",
"@hcaptcha/react-hcaptcha": "0.3.7",
"@prisma/client": "3.2.0",
"@sentry/browser": "6.13.2",
"@sentry/integrations": "6.13.2",
"@sentry/node": "6.13.2",
"@sentry/react": "6.13.2",
"@sentry/tracing": "6.13.2",
"@sentry/webpack-plugin": "1.17.2",
"@supabase/supabase-js": "1.24.0",
"@supabase/ui": "0.35.0",
"algoliasearch": "4.10.5",
"cheerio": "1.0.0-rc.10",
"cloudinary": "1.26.2",
"cloudinary": "1.27.0",
"clsx": "1.1.1",
"feed": "4.2.2",
"feedparser": "2.2.10",
"googleapis": "81.0.0",
"hcaptcha": "0.0.2",
"googleapis": "88.2.0",
"hcaptcha": "0.1.0",
"html-entities": "2.3.2",
"iconv-lite": "0.6.3",
"mailgun-js": "0.22.0",
"ms": "2.1.3",
"next": "11.0.1",
"next-seo": "4.26.0",
"next": "11.1.2",
"next-seo": "4.28.1",
"normalize.css": "8.0.1",
"pino": "6.11.3",
"pino": "6.13.3",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-instantsearch-dom": "6.12.0",
"rxjs": "7.2.0",
"slugify": "1.5.3",
"superjson": "1.7.4",
"react-instantsearch-dom": "6.12.1",
"rxjs": "7.3.1",
"slugify": "1.6.1",
"superjson": "1.7.5",
"xss": "1.0.9",
"yup": "0.32.9"
},
"devDependencies": {
"@next/bundle-analyzer": "11.0.1",
"@swc-node/jest": "1.3.1",
"@testing-library/dom": "8.1.0",
"@next/bundle-analyzer": "11.1.2",
"@swc-node/jest": "1.3.2",
"@testing-library/dom": "8.7.2",
"@testing-library/jest-dom": "5.14.1",
"@testing-library/react": "12.0.0",
"@testing-library/user-event": "13.1.9",
"@testing-library/react": "12.1.2",
"@testing-library/user-event": "13.2.1",
"@types/cheerio": "0.22.30",
"@types/feedparser": "2.2.4",
"@types/gtag.js": "0.0.7",
"@types/hcaptcha__react-hcaptcha": "0.1.5",
"@types/jest": "26.0.24",
"@types/lodash": "4.14.171",
"@types/lodash": "4.14.175",
"@types/mailgun-js": "0.22.12",
"@types/ms": "0.7.31",
"@types/node": "14.14.37",
"@types/pino": "6.3.9",
"@types/react": "17.0.14",
"@types/pino": "6.3.11",
"@types/react": "17.0.27",
"@types/react-dom": "17.0.9",
"@types/react-instantsearch-dom": "6.10.2",
"@types/react-instantsearch-dom": "6.12.0",
"@types/yup": "0.29.13",
"@types/zeit__next-source-maps": "0.0.2",
"@typescript-eslint/eslint-plugin": "4.28.2",
"@typescript-eslint/parser": "4.28.2",
"@typescript-eslint/eslint-plugin": "4.33.0",
"@typescript-eslint/parser": "4.33.0",
"all-contributors-cli": "6.20.0",
"babel-eslint": "10.1.0",
"babel-jest": "27.0.6",
"babel-plugin-superjson-next": "0.3.0",
"concurrently": "6.2.0",
"babel-plugin-superjson-next": "0.4.1",
"concurrently": "6.3.0",
"critters": "0.0.10",
"cross-env": "7.0.3",
"dotenv": "10.0.0",
"eslint": "7.30.0",
"eslint-config-next": "11.0.1",
"eslint": "7.32.0",
"eslint-config-next": "11.1.2",
"eslint-config-prettier": "8.3.0",
"eslint-config-react-app": "6.0.0",
"eslint-plugin-css-modules": "2.11.0",
"eslint-plugin-cypress": "2.11.3",
"eslint-plugin-flowtype": "5.8.0",
"eslint-plugin-functional": "3.2.1",
"eslint-plugin-import": "2.23.4",
"eslint-plugin-cypress": "2.12.1",
"eslint-plugin-flowtype": "6.1.0",
"eslint-plugin-functional": "3.7.2",
"eslint-plugin-import": "2.24.2",
"eslint-plugin-jsx-a11y": "6.4.1",
"eslint-plugin-react": "7.24.0",
"eslint-plugin-react": "7.26.1",
"eslint-plugin-react-hooks": "4.2.0",
"husky": "4.3.6",
"jest": "27.0.6",
"jest-css-modules": "2.1.0",
"jest-extended": "0.11.5",
"jest-watch-typeahead": "0.6.4",
"lint-staged": "11.0.0",
"lint-staged": "11.2.0",
"lodash": "4.17.21",
"msw": "0.31.0",
"pino-pretty": "5.1.1",
"msw": "0.35.0",
"pino-pretty": "7.0.1",
"postcss-flexbugs-fixes": "5.0.2",
"postcss-preset-env": "6.7.0",
"prettier": "2.3.2",
"prisma": "2.26.0",
"prettier": "2.4.1",
"prisma": "3.2.0",
"rimraf": "3.0.2",
"sass": "1.35.2",
"supabase": "0.4.4",
"ts-loader": "9.2.3",
"ts-node": "10.1.0",
"typescript": "4.3.5",
"sass": "1.42.1",
"supabase": "0.5.0",
"ts-loader": "9.2.6",
"ts-node": "10.2.1",
"typescript": "4.4.3",
"typescript-plugin-css-modules": "3.4.0",
"wait-on": "6.0.0"
},
Expand All @@ -141,9 +141,15 @@
"pre-commit": "lint-staged"
}
},
"resolutions": {
"eslint-plugin-import": "2.24.2",
"eslint-plugin-jsx-a11y": "6.4.1",
"eslint-plugin-react": "7.26.1",
"eslint-plugin-react-hooks": "4.2.0"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
"eslint --fix",
"eslint --resolve-plugins-relative-to . --fix",
"yarn prettier --write"
],
"*.{json,md,yaml,yml,scss,css}": [
Expand Down
1 change: 0 additions & 1 deletion pages/api/auth/index.ts
Expand Up @@ -3,6 +3,5 @@ import type { NextApiRequest, NextApiResponse } from 'next';
import { supabase } from '../../../utils/api/initSupabase';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
req.body = JSON.parse(req.body) as unknown;
supabase.auth.api.setAuthCookie(req, res);
}
43 changes: 34 additions & 9 deletions pages/login/index.tsx
@@ -1,33 +1,58 @@
import { Auth } from '@supabase/ui';
import { useEffect } from 'react';
import { useRouter } from 'next/router';
import { useEffect, useState } from 'react';

import { Layout } from '../../components/Layout';
import { supabase } from '../../utils/api/initSupabase';

type ViewType = Parameters<typeof Auth>[0]['view'];

export default function Login() {
const { user } = Auth.useUser();
const router = useRouter();
const [authView, setAuthView] = useState<ViewType>('sign_in');

useEffect(() => {
const { data: authListener } = supabase.auth.onAuthStateChange((event, session) => {
console.log('onAuthStateChange', { event, session });
if (event === 'PASSWORD_RECOVERY') {
setAuthView('update_password');
}
if (event === 'USER_UPDATED') {
setTimeout(() => setAuthView('sign_in'), 1000);
}

// headers: new Headers({ 'Content-Type': 'application/json', token }),
fetch('/api/auth', {
method: 'POST',
headers: new Headers({ 'Content-Type': 'application/json' }),
credentials: 'same-origin',
body: JSON.stringify({ event, session }),
}).catch((err) => console.error(err));
})
.then((res) => res.json())
.catch((err) => console.error(err));
});

return () => {
authListener?.unsubscribe();
};
}, []);

useEffect(() => {
if (user) {
void router.replace('/admin');
}
}, [user, router]);

return (
<Layout title="Panel admina">
{Auth({
supabaseClient: supabase,
providers: ['github'],
view: 'sign_in',
socialLayout: 'horizontal',
socialButtonSize: 'xlarge',
}) ?? null}
<Auth
supabaseClient={supabase}
providers={['github']}
view={authView}
socialLayout="horizontal"
socialButtonSize="xlarge"
/>
</Layout>
);
}

0 comments on commit a6cec67

Please sign in to comment.