feat: Add login stuff

This commit is contained in:
Xyphyn committed 2023-07-04 13:08:58 -07:00
1 parent f71ad4be8e
commit f0523b0c3f
18 files changed
+288 -56

No files matched your search

+2
View File
@@ -4,6 +4,7 @@
import { navigating, page } from '$app/stores'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'
import ToastContainer from '$lib/components/ui/toasts/ToastContainer.svelte'
nProgress.configure({
minimum: 0.4,
@@ -22,6 +23,7 @@
</script>
<Navbar />
<ToastContainer />
<div class="mx-auto max-w-5xl p-4">
<slot />
</div>
+1 -1
View File
@@ -14,7 +14,7 @@
PencilSquare,
UserGroup,
} from 'svelte-hero-icons'
import Avatar from './post/[id]/Avatar.svelte'
import Avatar from '../lib/components/ui/Avatar.svelte'
import Post from './post/[id]/Post.svelte'
import FormattedNumber from '$lib/components/util/FormattedNumber.svelte'
import Markdown from '$lib/components/markdown/Markdown.svelte'
+1 -1
View File
@@ -3,7 +3,7 @@
import Link from '$lib/components/input/Link.svelte'
import MultiSelect from '$lib/components/input/MultiSelect.svelte'
import SvelteMarkdown from 'svelte-markdown'
import Avatar from '../../post/[id]/Avatar.svelte'
import Avatar from '../../../lib/components/ui/Avatar.svelte'
import Post from '../../post/[id]/Post.svelte'
import { goto } from '$app/navigation'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
+3 -1
View File
@@ -1,5 +1,6 @@
import { getClient } from '$lib/lemmy.js'
import { authData, getClient } from '$lib/lemmy.js'
import type { ListingType, SortType } from 'lemmy-js-client'
import { get } from 'svelte/store'
export function load(req: any) {
const page = Number(req.url.searchParams.get('page') || 1) || 1
@@ -13,6 +14,7 @@ export function load(req: any) {
community_name: req.params.name,
page: page,
sort: sort,
auth: get(authData)?.token,
}),
streamed: {
community: getClient().getCommunity({
+2 -1
View File
@@ -12,6 +12,7 @@
import CommunityLink from '$lib/components/community/CommunityLink.svelte'
import Link from '$lib/components/input/Link.svelte'
import { Color } from '$lib/ui/colors.js'
import TextInput from '$lib/components/input/TextInput.svelte'
export let data
@@ -32,7 +33,7 @@
}}
/>
<div class="flex flex-col sm:flex-row gap-2 sm:ml-auto">
<input
<TextInput
class="bg-slate-100 border border-slate-200 dark:border-zinc-800
dark:bg-zinc-900 rounded-md"
bind:value={search}
+39 -4
View File
@@ -1,33 +1,63 @@
<script lang="ts">
import { goto } from '$app/navigation'
import Button from '$lib/components/input/Button.svelte'
import TextInput from '$lib/components/input/TextInput.svelte'
import Card from '$lib/components/ui/Card.svelte'
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
import { DEFAULT_INSTANCE_URL, authData, getClient } from '$lib/lemmy.js'
import { Color } from '$lib/ui/colors.js'
let data = {
instance: DEFAULT_INSTANCE_URL,
username: '',
password: '',
loading: false,
}
async function validateURL(instance: string): Promise<boolean> {
if (instance == '') return false
if (!instance.startsWith('https://')) {
instance = 'https://' + instance
}
const url = new URL(instance)
try {
await getClient(url.toString()).getSite({})
} catch (err) {
throw new Error('Invalid instance URL')
}
return true
}
async function logIn() {
data.loading = true
try {
const response = await getClient().login({
if (!(await validateURL(data.instance))) {
throw new Error('Invalid instance URL')
}
const response = await getClient(data.instance).login({
username_or_email: data.username,
password: data.password,
})
if (response.jwt) {
if (response?.jwt) {
authData.set({
instance: DEFAULT_INSTANCE_URL,
instance: data.instance,
token: response.jwt,
username: data.username,
})
addToast('Success', 'Successfully logged in.', ToastType.success)
goto('/')
} else {
throw new Error('Invalid credentials')
}
} catch (error) {
alert('failed')
addToast('Error', error as any, ToastType.error)
}
data.loading = false
}
@@ -36,6 +66,11 @@
<Card class="mx-auto p-6">
<form on:submit|preventDefault={logIn} class="flex flex-col gap-4">
<h1 class="font-bold text-lg">Log in</h1>
<TextInput
bind:value={data.instance}
label="Instance URL"
placeholder="lemmy.world"
/>
<TextInput
bind:value={data.username}
label="Username"
-8
View File
@@ -1,8 +0,0 @@
<script lang="ts">
export let url: string
export let alt: string = ''
export let width: number
</script>
<img src={url} {alt} {width} class="rounded-full aspect-square object-cover" />
+5 -5
View File
@@ -7,7 +7,7 @@
Minus,
Plus,
} from 'svelte-hero-icons'
import Avatar from '../Avatar.svelte'
import Avatar from '../../../../lib/components/ui/Avatar.svelte'
import type { CommentNodeI } from '../comments.js'
import SvelteMarkdown from 'svelte-markdown'
import RelativeDate from '$lib/components/util/RelativeDate.svelte'
@@ -27,6 +27,8 @@
const upvoted = node.comment_view.my_vote == 1
node.comment_view.my_vote = Number(!upvoted)
await getClient()
.likeComment({
score: upvoted ? 0 : 1,
@@ -34,8 +36,6 @@
comment_id: node.comment_view.comment.id,
})
.catch((_) => undefined)
node.comment_view.my_vote = Number(!upvoted)
}
async function downvote() {
@@ -43,6 +43,8 @@
const upvoted = node.comment_view.my_vote == -1
node.comment_view.my_vote = -Number(!upvoted)
await getClient()
.likeComment({
score: upvoted ? 0 : -1,
@@ -50,8 +52,6 @@
comment_id: node.comment_view.comment.id,
})
.catch((_) => undefined)
node.comment_view.my_vote = -Number(!upvoted)
}
</script>
@@ -4,6 +4,7 @@
import type { Comment, Post } from 'lemmy-js-client'
import { authData, getClient, user } from '$lib/lemmy.js'
import { createEventDispatcher } from 'svelte'
import { ToastType, addToast } from '$lib/components/ui/toasts/toasts.js'
export let postId: number
export let parentId: number | undefined = undefined
@@ -28,8 +29,13 @@
dispatch('comment', response)
newComment = ''
addToast(
'Success',
'Your comment was added. You may need to refresh to see it.',
ToastType.success
)
} catch (err) {
alert('Failed to comment')
addToast('Error', 'Failed to comment.', ToastType.error)
}
loading = false