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

Fix: unaccessible stores #120

Merged
merged 4 commits into from
Jan 15, 2023
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
2 changes: 1 addition & 1 deletion src/lib/components/Browse/LoginPrompt.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import IconSocialDiscordInverse from '$lib/assets/icons/social/IconSocialDiscordInverse.svelte'
import IconSocialGoogle from '$lib/assets/icons/social/IconSocialGoogle.svelte'
import IconSocialGitHubInverse from '$lib/assets/icons/social/IconSocialGitHubInverse.svelte'
import { getGitHubUrl, getDiscordUrl, getGoogleUrl } from '$lib/utils/authUrl'
// import { getGitHubUrl, getDiscordUrl, getGoogleUrl } from '$lib/utils/authUrl'
import { env } from '$env/dynamic/public'

async function getHref(provider: string) {
Expand Down
11 changes: 11 additions & 0 deletions src/lib/stores/authStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ function logout() {
window.location.href = '/'
}


function getJWT() {
return window.localStorage.getItem(JWT_KEY)
}

function getUserId() {
return window.localStorage.getItem('userId')
}

function setJWT({ jwt }: { jwt: string }) {
window.localStorage.setItem(JWT_KEY, jwt)
}
Expand Down Expand Up @@ -51,6 +60,8 @@ async function me() {

export {
logout,
getJWT,
getUserId,
setJWT,
setUserId,
setUser,
Expand Down
33 changes: 16 additions & 17 deletions src/lib/stores/chatStore.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { writable, type Writable } from 'svelte/store'
import { socketStore } from '$lib/stores/socketStore'
import { env } from '$env/dynamic/public'
import { userStore } from '$lib/stores/userStore'
import { authStore } from '$lib/stores/authStore'
import { channelStore } from '$lib/stores/channelStore'
// import { currentUser } from './userStore'
// import { currentChannel } from './channelStore'

class ChatStore {
public lastMessageSendDate: Date = new Date()
Expand Down Expand Up @@ -280,7 +279,7 @@ class ChatStore {
}

public async activateChatTab({ chat }: { chat: any }) {
const user = authStore.currentUser
// const user = authStore.currentUser
if (this.checkAlreadyExist(chat)) return
//TODO: get writable activeTabs
// this.activeTabs.push(chat)
Expand All @@ -289,7 +288,7 @@ class ChatStore {
}

public async activateGroupTab({ group }: { group: any }) {
const user = authStore.currentUser
// const user = authStore.currentUser
if (this.checkAlreadyExist(group)) return
//TODO: get writable activeTabs
// socketStore.emitChannelSubscribeByUser(group.channelId, user._id)
Expand All @@ -309,8 +308,8 @@ class ChatStore {
}

public async incomingMessageActivateChatTab(data: any) {
const user = authStore.currentUser
const otherUser = userStore.getUserById(data.source1)
// const user = authStore.currentUser
// const otherUser = userStore.getUserById(data.source1)
const chat = null
//TODO: get writable currentUser
// const existingChat = await this.getChat({
Expand Down Expand Up @@ -382,16 +381,16 @@ class ChatStore {
}

public async commitDeleteMessage({ oneVone, message }: { oneVone: boolean, message: any }) {
let chan = null
if (oneVone || !channelStore.currentChannel) {
chan = { _id: message.channelId } // if friend chat
} else {
chan = channelStore.currentChannel // if channel chat
}
if (chan) {
//TODO: get writable channel id
// this.deleteMessage({ message, channelId: chan._id })
}
// let chan = null
// if (oneVone || !channelStore.currentChannel) {
// chan = { _id: message.channelId } // if friend chat
// } else {
// chan = channelStore.currentChannel // if channel chat
// }
// if (chan) {
// //TODO: get writable channel id
// this.deleteMessage({ message, channelId: chan._id })
// }
}
}

Expand Down
44 changes: 22 additions & 22 deletions src/lib/stores/streamStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { writable, type Writable } from 'svelte/store'
import { env } from '$env/dynamic/public'
import { channelStore } from '$lib/stores/channelStore'
// import { currentChannel } from '$lib/stores/channelStore'
import { socketStore } from '$lib/stores/socketStore'

class StreamStore {
Expand Down Expand Up @@ -522,20 +522,20 @@ class StreamStore {
}

async leaveRoom() {
if (channelStore.currentChannel) {
this.disconnected()
this.stopObsStream()
this.stopScreenStream()
this.stopWebcamStream()
this.stopAudioStream()
//TODO: fix subscribring to room members writeable
// if (this.roomMembersSubscription) {
// this.roomMembersSubscription.unsubscribe()
// }
// if (this.userActionsSubscription) {
// this.userActionsSubscription.unsubscribe()
// }
}
// if (channelStore.currentChannel) {
this.disconnected()
this.stopObsStream()
this.stopScreenStream()
this.stopWebcamStream()
this.stopAudioStream()
//TODO: fix subscribring to room members writeable
// if (this.roomMembersSubscription) {
// this.roomMembersSubscription.unsubscribe()
// }
// if (this.userActionsSubscription) {
// this.userActionsSubscription.unsubscribe()
// }
// }
}

toggleRaiseHand() {
Expand Down Expand Up @@ -625,13 +625,13 @@ class StreamStore {
}

sendDataToRoom(message: any) {
if (channelStore.currentChannel) {
socketStore.emitUserActions({
channelId: '',//TODO: get channelId from writeable channelStore.currentChannel._id,
userData: this.userData,
message: JSON.stringify(message)
})
}
// if (channelStore.currentChannel) {
socketStore.emitUserActions({
channelId: '',//TODO: get channelId from writeable channelStore.currentChannel._id,
userData: this.userData,
message: JSON.stringify(message)
})
// }
}

public waitOneSecondObs() {
Expand Down
120 changes: 60 additions & 60 deletions src/lib/utils/authUrl.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
import {
PUBLIC_GOOGLE_OAUTH_CLIENT_ID,
PUBLIC_GOOGLE_OAUTH_REDIRECT_URL,
PUBLIC_GITHUB_OAUTH_CLIENT_ID,
PUBLIC_GITHUB_OAUTH_REDIRECT_URL,
PUBLIC_DISCORD_OAUTH_CLIENT_ID,
PUBLIC_DISCORD_OAUTH_REDIRECT_URL
} from '$env/static/public'

export const getGoogleUrl = (from: string) => {
const rootUrl = `https://accounts.google.com/o/oauth2/v2/auth`

const options = {
client_id: PUBLIC_GOOGLE_OAUTH_CLIENT_ID,
redirect_uri: PUBLIC_GOOGLE_OAUTH_REDIRECT_URL,
access_type: 'offline',
response_type: 'code',
prompt: 'consent',
scope: [
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/userinfo.email'
].join(' '),
state: from
}

const qs = new URLSearchParams(options)

return `${rootUrl}?${qs.toString()}`
}

export const getGitHubUrl = (from: string) => {
const rootURl = 'https://github.com/login/oauth/authorize'

const options = {
client_id: PUBLIC_GITHUB_OAUTH_CLIENT_ID,
redirect_uri: PUBLIC_GITHUB_OAUTH_REDIRECT_URL,
scope: 'user:email',
state: from
}

const qs = new URLSearchParams(options)

return `${rootURl}?${qs.toString()}`
}

export const getDiscordUrl = (from: string) => {
const rootURl = 'https://discord.com/api/oauth2/authorize'

const options = {
client_id: PUBLIC_DISCORD_OAUTH_CLIENT_ID,
redirect_uri: PUBLIC_DISCORD_OAUTH_REDIRECT_URL,
scope: 'identify email',
response_type: 'code',
state: from
}

const qs = new URLSearchParams(options)

return `${rootURl}?${qs.toString()}`
}
// import {
// PUBLIC_GOOGLE_OAUTH_CLIENT_ID,
// PUBLIC_GOOGLE_OAUTH_REDIRECT_URL,
// PUBLIC_GITHUB_OAUTH_CLIENT_ID,
// PUBLIC_GITHUB_OAUTH_REDIRECT_URL,
// PUBLIC_DISCORD_OAUTH_CLIENT_ID,
// PUBLIC_DISCORD_OAUTH_REDIRECT_URL
// } from '$env/static/public'

// export const getGoogleUrl = (from: string) => {
// const rootUrl = `https://accounts.google.com/o/oauth2/v2/auth`

// const options = {
// client_id: PUBLIC_GOOGLE_OAUTH_CLIENT_ID,
// redirect_uri: PUBLIC_GOOGLE_OAUTH_REDIRECT_URL,
// access_type: 'offline',
// response_type: 'code',
// prompt: 'consent',
// scope: [
// 'https://www.googleapis.com/auth/userinfo.profile',
// 'https://www.googleapis.com/auth/userinfo.email'
// ].join(' '),
// state: from
// }

// const qs = new URLSearchParams(options)

// return `${rootUrl}?${qs.toString()}`
// }

// export const getGitHubUrl = (from: string) => {
// const rootURl = 'https://github.com/login/oauth/authorize'

// const options = {
// client_id: PUBLIC_GITHUB_OAUTH_CLIENT_ID,
// redirect_uri: PUBLIC_GITHUB_OAUTH_REDIRECT_URL,
// scope: 'user:email',
// state: from
// }

// const qs = new URLSearchParams(options)

// return `${rootURl}?${qs.toString()}`
// }

// export const getDiscordUrl = (from: string) => {
// const rootURl = 'https://discord.com/api/oauth2/authorize'

// const options = {
// client_id: PUBLIC_DISCORD_OAUTH_CLIENT_ID,
// redirect_uri: PUBLIC_DISCORD_OAUTH_REDIRECT_URL,
// scope: 'identify email',
// response_type: 'code',
// state: from
// }

// const qs = new URLSearchParams(options)

// return `${rootURl}?${qs.toString()}`
// }
37 changes: 7 additions & 30 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
// @ts-ignore
import NProgress from 'nprogress'
import { browser } from '$app/environment'
import { onMount } from 'svelte'
import { navigating } from '$app/stores'
// Auth store
import { authStore } from '$lib/stores/authStore'
import { getJWT, getUserId, me, setJWT, setUserId, currentUser } from '$lib/stores/authStore'

// NProgress Loading bar
import 'nprogress/nprogress.css'
Expand Down Expand Up @@ -46,7 +44,6 @@
}
}

const { currentUser } = authStore
export let data: any

$: data.user, storeUserData()
Expand All @@ -58,20 +55,20 @@
token = data.user.token
userId = data.user.userId
} else {
token = authStore.getJWT()
userId = authStore.getUserId()
token = getJWT()
userId = getUserId()
}

if (token || userId) {
authStore.setJWT({ jwt: token })
authStore.setUserId({ userId })
authStore.me()
setJWT({ jwt: token })
setUserId({ userId })
me()
}
}
}

function logout() {
authStore.logout()
logout()
}
</script>

Expand Down Expand Up @@ -253,23 +250,3 @@
</div>
</div>
<!-- {/if} -->

<!-- <style global>
:root {
--color-default: #ffffff;
}
@media (prefers-color-scheme: dark) {
:root {
--my-color: #25252a;
}
}
[data-theme='dark'] {
--color-default: #25252a;
}
[data-theme='light'] {
--my-color: #ffffff;
}
body {
background-color: var(--color-default);
}
</style> -->
8 changes: 4 additions & 4 deletions src/routes/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { redirect } from '@sveltejs/kit'

/** @type {import('./$types').LayoutServerLoad} */
export function load({ locals }) {
if (!locals.user) {
throw redirect(307, '/browse')
}
export function load({ locals }: { locals: any }) {
if (!locals.user) {
throw redirect(307, '/browse')
}
}
Loading