Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: auth is ready #232

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode",
"Vue.volar",
"nrwl.angular-console",
"ms-playwright.playwright",
"ms-azuretools.vscode-docker",
"vitest.explorer",
"Codeium.codeium"
"Codeium.codeium",
"Vercel.turbo-vsc"
]
}
}
Expand Down
3 changes: 0 additions & 3 deletions .vscode/extensions.json

This file was deleted.

1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"prettier.enable": false,
"editor.formatOnSave": false,

"editor.codeActionsOnSave": {
Expand Down
7 changes: 3 additions & 4 deletions apps/website/app/components/MenuProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
class="profile-avatar"
@click="handleMenuClick"
>
<div>{{ user }}!</div>
<img src="~/assets/img/icons/twitch/112.png" alt="">
<img :src="user?.imageUrl ?? '/icons/twitch/112.png'" alt="">
</button>
<a v-else class="twitch" href="/api/auth/twitch">Войти</a>
</div>
</template>

<script setup lang="ts">
const { isFeedOpened } = useApp()
const { loggedIn, user } = useUserSession()
const { loggedIn, user, clear } = useUserSession()

function handleMenuClick() {
isFeedOpened.value = !isFeedOpened.value
clear()
}
</script>

Expand All @@ -43,7 +43,6 @@ function handleMenuClick() {
}

.profile-avatar {
padding: 0.2em;
width: 58px;
height: 58px;
background-color: var(--bronze-4);
Expand Down
2 changes: 0 additions & 2 deletions apps/website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"@twurple/eventsub-ws": "^7.1.0",
"@twurple/pubsub": "^7.1.0",
"howler": "^2.2.4",
"jsonwebtoken": "^9.0.2",
"pixi.js": "~8.2.6",
"zod": "^3.23.8"
},
Expand All @@ -36,7 +35,6 @@
"@nuxt/devtools": "^1.3.9",
"@nuxt/kit": "^3.10.0",
"@types/howler": "^2.2.11",
"@types/jsonwebtoken": "^9.0.6",
"@types/node": "^20.14.8",
"@vueuse/core": "^11.0.1",
"@vueuse/nuxt": "^11.0.1",
Expand Down
Binary file added apps/website/public/icons/twitch/112.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/website/public/icons/twitch/28.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added apps/website/public/icons/twitch/56.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 4 additions & 20 deletions apps/website/server/api/auth/me.get.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
import jwt from 'jsonwebtoken'
import type { WebsiteProfile } from '@chat-game/types'
export default defineEventHandler(async (event) => {
const session = await getUserSession(event)

export default defineEventHandler((event) => {
const { public: publicEnv, jwtSecretKey } = useRuntimeConfig()

const token = getCookie(event, publicEnv.cookieKey)
if (!token) {
if (!session?.user) {
throw createError({
statusCode: 401,
statusMessage: 'Unauthorized',
})
}

try {
const { profile } = jwt.verify(token, jwtSecretKey) as { profile: WebsiteProfile }
return profile
} catch (error) {
if (error instanceof jwt.TokenExpiredError) {
deleteCookie(event, publicEnv.cookieKey, { path: '/' })
}
}

throw createError({
statusCode: 401,
statusMessage: 'Unauthorized',
})
return session.user
})
15 changes: 0 additions & 15 deletions apps/website/server/api/auth/sign-out.delete.ts

This file was deleted.

33 changes: 28 additions & 5 deletions apps/website/server/api/auth/twitch.get.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
const logger = useLogger('twitch-auth')

interface TwitchUser {
id: string
login: string
display_name: string
type: string
broadcaster_type: 'affiliate' | 'partner'
description: string
profile_image_url: string
offline_image_url: string
view_count: number
email: string
created_at: Date
}

export default oauthTwitchEventHandler({
config: {
emailRequired: true,
},
async onSuccess(event, { user }) {
logger.log(JSON.stringify(user))
async onSuccess(event, result: { user: TwitchUser }) {
logger.success(JSON.stringify(result.user))

const repository = new DBRepository()

const profileInDB = await repository.findOrCreateProfile({
userId: result.user.id,
userName: result.user.login,
})

await setUserSession(event, {
user: {
id: user.id,
twitchId: user.id,
userName: user.userName,
id: profileInDB.id,
twitchId: profileInDB.twitchId,
userName: profileInDB.userName,
imageUrl: result.user.profile_image_url,
},
})

return sendRedirect(event, '/')
},
// Optional, will return a json error and 401 status code by default
Expand Down
71 changes: 0 additions & 71 deletions apps/website/server/api/auth/twitch3.get.ts

This file was deleted.

4 changes: 4 additions & 0 deletions apps/website/server/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export default defineEventHandler((event) => {
return
}

if (event.path === '/api/_auth/session') {
return
}

if (!token || token !== `Bearer ${websiteBearer}`) {
return createError({
statusCode: 403,
Expand Down
3 changes: 2 additions & 1 deletion apps/website/types/auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
declare module '#auth-utils' {
interface User {
id: string
twitchId: number
twitchId: string
userName: string
imageUrl: string
}

interface UserSession {
Expand Down
Loading
Loading