diff --git a/bun.lockb b/bun.lockb index 092cf2e..260f147 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 82eb00c..f96ad72 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "lint": "next lint" }, "dependencies": { + "solar-js-sdk": "./packages/sn", "@emotion/cache": "^11.14.0", "@emotion/react": "^11.14.0", "@emotion/server": "^11.11.0", diff --git a/packages/sn/.gitignore b/packages/sn/.gitignore new file mode 100644 index 0000000..9b1ee42 --- /dev/null +++ b/packages/sn/.gitignore @@ -0,0 +1,175 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/packages/sn/bun.lockb b/packages/sn/bun.lockb new file mode 100755 index 0000000..ef154a0 Binary files /dev/null and b/packages/sn/bun.lockb differ diff --git a/packages/sn/package.json b/packages/sn/package.json new file mode 100644 index 0000000..8ac21df --- /dev/null +++ b/packages/sn/package.json @@ -0,0 +1,32 @@ +{ + "name": "solar-js-sdk", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "type": "module", + "tsup": { + "entry": [ + "src/index.ts" + ], + "splitting": true, + "sourcemap": true, + "clean": true, + "dts": true, + "format": "esm" + }, + "scripts": { + "build": "tsup" + }, + "devDependencies": { + "@types/bun": "latest", + "tsup": "^8.3.5" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "dependencies": { + "axios": "^1.7.9", + "axios-case-converter": "^1.1.1", + "universal-cookie": "^7.2.2", + "zustand": "^5.0.3" + } +} diff --git a/src/services/attachment.ts b/packages/sn/src/attachment.ts similarity index 100% rename from src/services/attachment.ts rename to packages/sn/src/attachment.ts diff --git a/src/services/auth.ts b/packages/sn/src/auth.ts similarity index 65% rename from src/services/auth.ts rename to packages/sn/src/auth.ts index 414b3d3..d2a73f1 100644 --- a/src/services/auth.ts +++ b/packages/sn/src/auth.ts @@ -1,4 +1,4 @@ -import { hasCookie } from 'cookies-next/client' +import Cookies from 'universal-cookie' export interface SnAuthResult { isFinished: boolean @@ -35,8 +35,21 @@ export interface SnAuthFactor { accountId?: number | null } +export function setTokenCookies(atk: string, rtk: string) { + const cookies = new Cookies() + cookies.set('nex_user_atk', atk, { path: '/', maxAge: 2592000 }) + cookies.set('nex_user_rtk', rtk, { path: '/', maxAge: 2592000 }) +} + +export function removeTokenCookies() { + const cookies = new Cookies() + cookies.remove('nex_user_atk') + cookies.remove('nex_user_rtk') +} + export function checkAuthenticatedClient(): boolean { - return !!hasCookie('nex_user_atk') + const cookies = new Cookies() + return !!cookies.get('nex_user_atk') } export function redirectToLogin() { diff --git a/src/services/checkIn.ts b/packages/sn/src/checkIn.ts similarity index 100% rename from src/services/checkIn.ts rename to packages/sn/src/checkIn.ts diff --git a/packages/sn/src/index.ts b/packages/sn/src/index.ts new file mode 100644 index 0000000..9d24474 --- /dev/null +++ b/packages/sn/src/index.ts @@ -0,0 +1,7 @@ +export * from './matrix/product' +export * from './attachment' +export * from './auth' +export * from './checkIn' +export * from './network' +export * from './post' +export * from './user' diff --git a/src/services/matrix/product.ts b/packages/sn/src/matrix/product.ts similarity index 100% rename from src/services/matrix/product.ts rename to packages/sn/src/matrix/product.ts diff --git a/src/services/network.ts b/packages/sn/src/network.ts similarity index 79% rename from src/services/network.ts rename to packages/sn/src/network.ts index 1fa2267..d08a952 100644 --- a/src/services/network.ts +++ b/packages/sn/src/network.ts @@ -1,6 +1,7 @@ -import axios, { AxiosInstance } from 'axios' +import axios, { type AxiosInstance } from 'axios' import applyCaseMiddleware from 'axios-case-converter' -import { hasCookie, getCookie, setCookie } from 'cookies-next/client' +import Cookies from 'universal-cookie' +import { setTokenCookies } from './auth' const baseURL = 'https://api.sn.solsynth.dev' @@ -26,23 +27,23 @@ export const sni: AxiosInstance = (() => { })() async function refreshToken(): Promise { - if (!hasCookie('nex_user_atk') || !hasCookie('nex_user_rtk')) return + const cookies = new Cookies() + if (!cookies.get('nex_user_atk') || !cookies.get('nex_user_rtk')) return - const ogTk: string = getCookie('nex_user_atk')! + const ogTk: string = cookies.get('nex_user_atk')! if (!isTokenExpired(ogTk)) return ogTk const resp = await axios.post( '/cgi/id/auth/token', { - refresh_token: getCookie('nex_user_rtk')!, + refresh_token: cookies.get('nex_user_rtk')!, grant_type: 'refresh_token', }, { baseURL }, ) const atk: string = resp.data['access_token'] const rtk: string = resp.data['refresh_token'] - setCookie('nex_user_atk', atk, { path: '/', maxAge: 2592000 }) - setCookie('nex_user_rtk', rtk, { path: '/', maxAge: 2592000 }) + setTokenCookies(atk, rtk) console.log('[Authenticator] Refreshed token...') diff --git a/src/services/post.ts b/packages/sn/src/post.ts similarity index 100% rename from src/services/post.ts rename to packages/sn/src/post.ts diff --git a/packages/sn/src/user.ts b/packages/sn/src/user.ts new file mode 100644 index 0000000..a2ca8e4 --- /dev/null +++ b/packages/sn/src/user.ts @@ -0,0 +1,83 @@ +import { create } from 'zustand' +import { sni } from './network' +import Cookies from 'universal-cookie' + +export interface SnAccount { + id: number + createdAt: Date + updatedAt: Date + deletedAt?: Date | null + confirmedAt?: Date | null + contacts?: SnAccountContact[] | null + avatar: string + banner: string + description: string + name: string + nick: string + permNodes: Record + profile?: SnAccountProfile | null + badges: SnAccountBadge[] + suspendedAt?: Date | null + affiliatedId?: number | null + affiliatedTo?: number | null + automatedBy?: number | null + automatedId?: number | null +} + +export interface SnAccountContact { + accountId: number + content: string + createdAt: Date + deletedAt?: Date | null + id: number + isPrimary: boolean + isPublic: boolean + type: number + updatedAt: Date + verifiedAt?: Date | null +} + +export interface SnAccountProfile { + id: number + accountId: number + birthday?: Date | null + createdAt: Date + deletedAt?: Date | null + experience: number + firstName: string + lastName: string + lastSeenAt?: Date | null + updatedAt: Date +} + +export interface SnAccountBadge { + id: number + createdAt: Date + updatedAt: Date + deletedAt?: Date | null + type: string + accountId: number + metadata: Record +} + +export interface UserStore { + account: SnAccount | undefined + fetchUser: () => Promise +} + +export const useUserStore = create((set) => ({ + account: undefined, + fetchUser: async (): Promise => { + const cookies = new Cookies() + if (!cookies.get('nex_user_atk')) return + try { + const resp = await sni.get('/cgi/id/users/me') + set({ account: resp.data }) + console.log('[Authenticator] Logged in as @' + resp.data.name) + return resp.data + } catch (err) { + console.error('[Authenticator] Unable to get user profile: ', err) + return + } + }, +})) diff --git a/packages/sn/tsconfig.json b/packages/sn/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/packages/sn/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +} diff --git a/src/components/CapAppBar.tsx b/src/components/CapAppBar.tsx index b5e9d14..4a331bf 100644 --- a/src/components/CapAppBar.tsx +++ b/src/components/CapAppBar.tsx @@ -1,6 +1,6 @@ -import { useUserStore } from '@/services/user' +import { useUserStore } from 'solar-js-sdk' import { AppBar, AppBarProps, Avatar, IconButton, Toolbar, Typography, useScrollTrigger, useTheme } from '@mui/material' -import { getAttachmentUrl } from '@/services/network' +import { getAttachmentUrl } from 'solar-js-sdk' import MenuIcon from '@mui/icons-material/Menu' import AccountCircle from '@mui/icons-material/AccountCircle' import Link from 'next/link' diff --git a/src/components/attachments/AttachmentItem.tsx b/src/components/attachments/AttachmentItem.tsx index 8dbc60a..c0d86a4 100644 --- a/src/components/attachments/AttachmentItem.tsx +++ b/src/components/attachments/AttachmentItem.tsx @@ -1,5 +1,5 @@ -import { SnAttachment } from '@/services/attachment' -import { getAttachmentUrl } from '@/services/network' +import { SnAttachment } from 'solar-js-sdk' +import { getAttachmentUrl } from 'solar-js-sdk' import { QuestionMark } from '@mui/icons-material' import { Link, Paper, Typography } from '@mui/material' import { ComponentProps } from 'react' diff --git a/src/components/auth/SnLoginCheckpoint.tsx b/src/components/auth/SnLoginCheckpoint.tsx index b60c0eb..f5bead7 100644 --- a/src/components/auth/SnLoginCheckpoint.tsx +++ b/src/components/auth/SnLoginCheckpoint.tsx @@ -1,14 +1,13 @@ 'use client' -import { SnAuthFactor, SnAuthResult, SnAuthTicket } from '@/services/auth' -import { sni } from '@/services/network' +import { setTokenCookies, SnAuthFactor, SnAuthResult, SnAuthTicket } from 'solar-js-sdk' +import { sni } from 'solar-js-sdk' import { ArrowForward } from '@mui/icons-material' import { Collapse, Alert, Box, TextField, Button } from '@mui/material' import { useState } from 'react' import { useForm } from 'react-hook-form' import ErrorIcon from '@mui/icons-material/Error' -import { setCookie } from 'cookies-next/client' export interface SnLoginCheckpointForm { password: string @@ -44,8 +43,7 @@ export function SnLoginCheckpoint({ }) const atk: string = tokenResp.data['accessToken'] const rtk: string = tokenResp.data['refreshToken'] - setCookie('nex_user_atk', atk, { path: '/', maxAge: 2592000 }) - setCookie('nex_user_rtk', rtk, { path: '/', maxAge: 2592000 }) + setTokenCookies(atk, rtk) console.log('[Authenticator] User has been logged in. Result atk: ', atk) } diff --git a/src/components/auth/SnLoginRouter.tsx b/src/components/auth/SnLoginRouter.tsx index 68f4ec3..fe566ad 100644 --- a/src/components/auth/SnLoginRouter.tsx +++ b/src/components/auth/SnLoginRouter.tsx @@ -1,7 +1,7 @@ 'use client' -import { SnAuthFactor, SnAuthTicket } from '@/services/auth' -import { sni } from '@/services/network' +import { SnAuthFactor, SnAuthTicket } from 'solar-js-sdk' +import { sni } from 'solar-js-sdk' import { Collapse, Alert, Box, Button, Typography, ButtonGroup } from '@mui/material' import { useState } from 'react' diff --git a/src/components/auth/SnLoginStart.tsx b/src/components/auth/SnLoginStart.tsx index e54aeb6..402d5ee 100644 --- a/src/components/auth/SnLoginStart.tsx +++ b/src/components/auth/SnLoginStart.tsx @@ -1,10 +1,10 @@ 'use client' import { useState } from 'react' -import { sni } from '@/services/network' +import { sni } from 'solar-js-sdk' import { ArrowForward } from '@mui/icons-material' import { Alert, Box, Button, Collapse, Link, TextField, Typography } from '@mui/material' -import { SnAuthFactor, SnAuthResult, SnAuthTicket } from '@/services/auth' +import { SnAuthFactor, SnAuthResult, SnAuthTicket } from 'solar-js-sdk' import { useForm } from 'react-hook-form' import NextLink from 'next/link' diff --git a/src/components/layouts/ConsoleLayout.tsx b/src/components/layouts/ConsoleLayout.tsx index 80ffa46..594d3bd 100644 --- a/src/components/layouts/ConsoleLayout.tsx +++ b/src/components/layouts/ConsoleLayout.tsx @@ -1,4 +1,4 @@ -import { checkAuthenticatedClient, redirectToLogin } from '@/services/auth' +import { checkAuthenticatedClient, redirectToLogin } from 'solar-js-sdk' import { JSX, useEffect } from 'react' import { DashboardLayout, Navigation } from '@toolpad/core' import { Box, Stack, Typography } from '@mui/material' diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 1099456..5cb3168 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -5,7 +5,7 @@ import { Roboto } from 'next/font/google' import { CapAppBar } from '@/components/CapAppBar' import { PagesProgressBar as ProgressBar } from 'next-nprogress-bar' import { AppProvider } from '@toolpad/core/nextjs' -import { useUserStore } from '@/services/user' +import { useUserStore } from 'solar-js-sdk' import { useEffect } from 'react' import Head from 'next/head' diff --git a/src/pages/attachments/index.tsx b/src/pages/attachments/index.tsx index 177d0a0..f9e62f5 100644 --- a/src/pages/attachments/index.tsx +++ b/src/pages/attachments/index.tsx @@ -1,6 +1,6 @@ import { AttachmentItem } from '@/components/attachments/AttachmentItem' -import { SnAttachment } from '@/services/attachment' -import { sni } from '@/services/network' +import { SnAttachment } from 'solar-js-sdk' +import { sni } from 'solar-js-sdk' import { Box, ImageList, ImageListItem, Pagination, useMediaQuery, useTheme } from '@mui/material' import { GetServerSideProps, InferGetServerSidePropsType } from 'next' import { useRouter } from 'next/router' diff --git a/src/pages/auth/authorize.tsx b/src/pages/auth/authorize.tsx index 817f06d..b2c1639 100644 --- a/src/pages/auth/authorize.tsx +++ b/src/pages/auth/authorize.tsx @@ -1,8 +1,8 @@ -import { sni } from '@/services/network' +import { sni } from 'solar-js-sdk' import { Container, Box, Typography, Alert, Collapse, Button, CircularProgress, Card, CardContent } from '@mui/material' import { useRouter } from 'next/router' import { useEffect, useState } from 'react' -import { checkAuthenticatedClient, redirectToLogin, SnAuthTicket } from '@/services/auth' +import { checkAuthenticatedClient, redirectToLogin, SnAuthTicket } from 'solar-js-sdk' import ErrorIcon from '@mui/icons-material/Error' import CloseIcon from '@mui/icons-material/Close' diff --git a/src/pages/auth/login.tsx b/src/pages/auth/login.tsx index 65d30fc..b74ca61 100644 --- a/src/pages/auth/login.tsx +++ b/src/pages/auth/login.tsx @@ -1,8 +1,8 @@ import { SnLoginCheckpoint } from '@/components/auth/SnLoginCheckpoint' import { SnLoginRouter } from '@/components/auth/SnLoginRouter' import { SnLoginStart } from '@/components/auth/SnLoginStart' -import { SnAuthFactor, SnAuthTicket } from '@/services/auth' -import { useUserStore } from '@/services/user' +import { SnAuthFactor, SnAuthTicket } from 'solar-js-sdk' +import { useUserStore } from 'solar-js-sdk' import { Box, Container, Typography } from '@mui/material' import { useRouter } from 'next/router' import { useState } from 'react' diff --git a/src/pages/console/matrix/index.tsx b/src/pages/console/matrix/index.tsx index 6b9a861..bc13e0e 100644 --- a/src/pages/console/matrix/index.tsx +++ b/src/pages/console/matrix/index.tsx @@ -1,17 +1,7 @@ import { ConsoleLayout, getConsoleStaticProps } from '@/components/layouts/ConsoleLayout' -import { MaProduct } from '@/services/matrix/product' -import { sni } from '@/services/network' -import { - Typography, - Container, - Box, - Button, - Grid2 as Grid, - Card, - CardActionArea, - CardContent, - CardActions, -} from '@mui/material' +import { MaProduct } from 'solar-js-sdk' +import { sni } from 'solar-js-sdk' +import { Typography, Container, Box, Button, Grid2 as Grid, Card, CardContent, CardActions } from '@mui/material' import NextLink from 'next/link' import { useEffect, useState } from 'react' diff --git a/src/pages/console/matrix/products/new.tsx b/src/pages/console/matrix/products/new.tsx index 5376c62..834f87d 100644 --- a/src/pages/console/matrix/products/new.tsx +++ b/src/pages/console/matrix/products/new.tsx @@ -3,8 +3,8 @@ import { Typography, Container, Box, Button, TextField, Collapse, Alert } from ' import { useForm } from 'react-hook-form' import { useState } from 'react' import { useRouter } from 'next/router' +import { sni } from 'solar-js-sdk' import NextLink from 'next/link' -import { sni } from '@/services/network' import ErrorIcon from '@mui/icons-material/Error' diff --git a/src/pages/flow/accounts/confirm.tsx b/src/pages/flow/accounts/confirm.tsx index ea05301..780fc9a 100644 --- a/src/pages/flow/accounts/confirm.tsx +++ b/src/pages/flow/accounts/confirm.tsx @@ -1,4 +1,4 @@ -import { sni } from '@/services/network' +import { sni } from 'solar-js-sdk' import { Container, Box, Typography, CircularProgress, Alert, Collapse } from '@mui/material' import { useRouter } from 'next/router' import { useEffect, useState } from 'react' diff --git a/src/pages/flow/accounts/deletion.tsx b/src/pages/flow/accounts/deletion.tsx index 47edc16..6089809 100644 --- a/src/pages/flow/accounts/deletion.tsx +++ b/src/pages/flow/accounts/deletion.tsx @@ -1,4 +1,4 @@ -import { sni } from '@/services/network' +import { sni } from 'solar-js-sdk' import { Container, Box, Typography, Alert, Collapse, Button } from '@mui/material' import { useRouter } from 'next/router' import { useState } from 'react' diff --git a/src/pages/flow/accounts/password-reset.tsx b/src/pages/flow/accounts/password-reset.tsx index 3473863..822c21e 100644 --- a/src/pages/flow/accounts/password-reset.tsx +++ b/src/pages/flow/accounts/password-reset.tsx @@ -1,4 +1,4 @@ -import { sni } from '@/services/network' +import { sni } from 'solar-js-sdk' import { Container, Box, Typography, Alert, Collapse, Button, TextField } from '@mui/material' import { useRouter } from 'next/router' import { useState } from 'react' diff --git a/src/pages/posts/[...id].tsx b/src/pages/posts/[...id].tsx index 1d473e2..8038ff3 100644 --- a/src/pages/posts/[...id].tsx +++ b/src/pages/posts/[...id].tsx @@ -1,6 +1,6 @@ -import { getAttachmentUrl, sni } from '@/services/network' -import { SnPost } from '@/services/post' -import { listAttachment, SnAttachment } from '@/services/attachment' +import { getAttachmentUrl, sni } from 'solar-js-sdk' +import { SnPost } from 'solar-js-sdk' +import { listAttachment, SnAttachment } from 'solar-js-sdk' import { Grid2 as Grid, Alert, diff --git a/src/pages/posts/feed.ts b/src/pages/posts/feed.ts index 7ba8b71..a48cd59 100644 --- a/src/pages/posts/feed.ts +++ b/src/pages/posts/feed.ts @@ -1,5 +1,5 @@ -import { sni } from '@/services/network' -import { SnPost } from '@/services/post' +import { sni } from 'solar-js-sdk' +import { SnPost } from 'solar-js-sdk' import { GetServerSideProps } from 'next' import { Feed } from 'feed' diff --git a/src/pages/posts/index.tsx b/src/pages/posts/index.tsx index 99d49d9..c8206e7 100644 --- a/src/pages/posts/index.tsx +++ b/src/pages/posts/index.tsx @@ -1,7 +1,7 @@ import { AttachmentItem } from '@/components/attachments/AttachmentItem' -import { SnAttachment, listAttachment } from '@/services/attachment' -import { getAttachmentUrl, sni } from '@/services/network' -import { SnPost } from '@/services/post' +import { SnAttachment, listAttachment } from 'solar-js-sdk' +import { getAttachmentUrl, sni } from 'solar-js-sdk' +import { SnPost } from 'solar-js-sdk' import { Avatar, Box, Container, Divider, Grid2 as Grid, Link, Pagination, Paper, Typography } from '@mui/material' import { GetServerSideProps, InferGetServerSidePropsType } from 'next' import NextLink from 'next/link' diff --git a/src/pages/posts/sitemap.xml.ts b/src/pages/posts/sitemap.xml.ts index 63a2677..4205d44 100644 --- a/src/pages/posts/sitemap.xml.ts +++ b/src/pages/posts/sitemap.xml.ts @@ -1,5 +1,5 @@ -import { sni } from '@/services/network' -import { SnPost } from '@/services/post' +import { sni } from 'solar-js-sdk' +import { SnPost } from 'solar-js-sdk' import { GetServerSideProps } from 'next' import { EnumChangefreq, SitemapItem, SitemapStream, streamToPromise } from 'sitemap' import { Readable } from 'stream' diff --git a/src/pages/users/[name].tsx b/src/pages/users/[name].tsx index 015ee89..a0e44ea 100644 --- a/src/pages/users/[name].tsx +++ b/src/pages/users/[name].tsx @@ -1,9 +1,10 @@ -import { SnCheckInRecord } from '@/services/checkIn' -import { getAttachmentUrl, sni } from '@/services/network' -import { SnAccount, SnAccountBadgeMapping } from '@/services/user' +import { SnCheckInRecord } from 'solar-js-sdk' +import { getAttachmentUrl, sni } from 'solar-js-sdk' +import { SnAccount } from 'solar-js-sdk' import { Avatar, Box, Card, CardContent, Container, Grid2 as Grid, Typography } from '@mui/material' import { LineChart } from '@mui/x-charts' import type { InferGetServerSidePropsType, GetServerSideProps } from 'next' +import { SnAccountBadgeMapping } from '@/services/user' import Image from 'next/image' export const getServerSideProps = (async (context) => { diff --git a/src/pages/users/me.tsx b/src/pages/users/me.tsx index 93e2ef5..42d887d 100644 --- a/src/pages/users/me.tsx +++ b/src/pages/users/me.tsx @@ -1,14 +1,14 @@ -import { checkAuthenticatedClient, redirectToLogin } from '@/services/auth' -import { useUserStore } from '@/services/user' +import { checkAuthenticatedClient, redirectToLogin } from 'solar-js-sdk' +import { useUserStore } from 'solar-js-sdk' import { Avatar, Box, Button, Container, Typography } from '@mui/material' -import { getAttachmentUrl } from '@/services/network' +import { getAttachmentUrl } from 'solar-js-sdk' import { useEffect } from 'react' +import { removeTokenCookies } from 'solar-js-sdk' import Image from 'next/image' import LogoutIcon from '@mui/icons-material/Logout' import LaunchIcon from '@mui/icons-material/Launch' import Link from 'next/link' -import { deleteCookie } from 'cookies-next/client' export default function UserItself() { useEffect(() => { @@ -18,8 +18,7 @@ export default function UserItself() { const userStore = useUserStore() function logout() { - deleteCookie('nex_user_atk') - deleteCookie('nex_user_rtk') + removeTokenCookies() window.location.reload() } diff --git a/src/services/user.tsx b/src/services/user.tsx index bb6aa40..8b1bed3 100644 --- a/src/services/user.tsx +++ b/src/services/user.tsx @@ -1,69 +1,8 @@ -import { create } from 'zustand' -import { sni } from './network' -import { hasCookie } from 'cookies-next/client' import { JSX } from 'react' import ConstructionIcon from '@mui/icons-material/Construction' import FlagIcon from '@mui/icons-material/Flag' -export interface SnAccount { - id: number - createdAt: Date - updatedAt: Date - deletedAt?: Date | null - confirmedAt?: Date | null - contacts?: SnAccountContact[] | null - avatar: string - banner: string - description: string - name: string - nick: string - permNodes: Record - profile?: SnAccountProfile | null - badges: SnAccountBadge[] - suspendedAt?: Date | null - affiliatedId?: number | null - affiliatedTo?: number | null - automatedBy?: number | null - automatedId?: number | null -} - -export interface SnAccountContact { - accountId: number - content: string - createdAt: Date - deletedAt?: Date | null - id: number - isPrimary: boolean - isPublic: boolean - type: number - updatedAt: Date - verifiedAt?: Date | null -} - -export interface SnAccountProfile { - id: number - accountId: number - birthday?: Date | null - createdAt: Date - deletedAt?: Date | null - experience: number - firstName: string - lastName: string - lastSeenAt?: Date | null - updatedAt: Date -} - -export interface SnAccountBadge { - id: number - createdAt: Date - updatedAt: Date - deletedAt?: Date | null - type: string - accountId: number - metadata: Record -} - export const SnAccountBadgeMapping: Record = { 'company.staff': { icon: , @@ -74,24 +13,3 @@ export const SnAccountBadgeMapping: Record Promise -} - -export const useUserStore = create((set) => ({ - account: undefined, - fetchUser: async (): Promise => { - if (!hasCookie('nex_user_atk')) return - try { - const resp = await sni.get('/cgi/id/users/me') - set({ account: resp.data }) - console.log('[Authenticator] Logged in as @' + resp.data.name) - return resp.data - } catch (err) { - console.error('[Authenticator] Unable to get user profile: ', err) - return - } - }, -}))