Skip to content

Commit 39b9ba4

Browse files
Move code snippets to separate files in functions and auth folders
This commit moves code snippets from inline strings into separate text files for both the functions and auth components. The code snippets are now imported from dedicated files rather than being defined directly in the components.
1 parent 2ff75f3 commit 39b9ba4

File tree

19 files changed

+524
-509
lines changed

19 files changed

+524
-509
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Client, Account } from 'node-appwrite';
2+
3+
async function getLoggedInUser(context) {
4+
const session = cookies().get('custom-session-cookie');
5+
if (!session) return;
6+
7+
const client = new Client()
8+
.setEndpoint(import.meta.env.PUBLIC_APPWRITE_ENDPOINT)
9+
.setProject(import.meta.env.PUBLIC_APPWRITE_PROJECT_ID);
10+
11+
client.setSession(session.value);
12+
const account = new Account(client);
13+
14+
return account.get();
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Client, Account } from 'node-appwrite';
2+
import { cookies } from 'next/headers';
3+
4+
async function getLoggedInUser() {
5+
const session = cookies().get('custom-session-cookie');
6+
if (!session) return;
7+
8+
const client = new Client()
9+
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
10+
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID);
11+
12+
client.setSession(session.value);
13+
const account = new Account(client);
14+
15+
return account.get();
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Client, Account } from 'node-appwrite';
2+
import { H3Event, getCookie } from 'h3';
3+
4+
async function getLoggedInUser(event) {
5+
const session = getCookie(event, 'custom-session-cookie');
6+
if (!session) return;
7+
8+
const client = new Client()
9+
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
10+
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID);
11+
12+
client.setSession(session.value);
13+
const account = new Account(client);
14+
15+
return account.get();
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Client, Account } from 'node-appwrite';
2+
import { createCookie } from '@remix-run/node';
3+
4+
export const customSessionCookie = createCookie('custom-session-cookie', {
5+
maxAge: 604800,
6+
});
7+
8+
async function getLoggedInUser(request) {
9+
const cookies = request.headers.get('Cookie');
10+
const session = await customSessionCookie.parse(cookies):
11+
if (!session) return;
12+
13+
const client = new Client()
14+
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
15+
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID);
16+
17+
client.setSession(session.value);
18+
const account = new Account(client);
19+
20+
return await account.get();
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Client, Account } from 'node-appwrite';
2+
3+
async function getLoggedInUser() {
4+
const session = cookies().get('custom-session-cookie');
5+
if (!session) return;
6+
7+
const client = new Client()
8+
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
9+
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID);
10+
11+
client.setSession(session.value);
12+
const account = new Account(client);
13+
14+
return account.get();
15+
}

src/routes/products/auth/(components)/SSR.svelte

Lines changed: 10 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,40 @@
11
<script lang="ts">
2-
import { PUBLIC_APPWRITE_DASHBOARD } from '$env/static/public';
32
import { trackEvent } from '$lib/actions/analytics';
43
import { Tooltip } from '$lib/components';
54
import { Button } from '$lib/components/ui';
65
import { Framework, Platform } from '$lib/utils/references';
76
import MultiFrameworkCode from './MultiFrameworkCode.svelte';
7+
import SnippetNextJs from './(snippets)/nextjs.txt';
8+
import SnippetSvelteKit from './(snippets)/sveltekit.txt';
9+
import SnippetAstro from './(snippets)/astro.txt';
10+
import SnippetNuxt from './(snippets)/nuxt.txt';
11+
import SnippetRemix from './(snippets)/remix.txt';
812
913
const codeSnippets = [
1014
{
1115
language: Platform.ClientWeb,
1216
platform: Framework.NextJs,
13-
content: `import { Client, Account } from 'node-appwrite'
14-
import { cookies } from 'next/headers'
15-
16-
async function getLoggedInUser() {
17-
const session = cookies().get("custom-session-cookie");
18-
if (!session) return
19-
20-
const client = new Client()
21-
.setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
22-
.setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID)
23-
24-
client.setSession(session.value)
25-
const account = new Account(client);
26-
27-
return await account.get()
28-
}
29-
`
17+
content: SnippetNextJs
3018
},
3119
{
3220
language: Platform.ClientWeb,
3321
platform: Framework.SvelteKit,
34-
content: `import { Client, Account } from 'node-appwrite'
35-
36-
async function getLoggedInUser() {
37-
const session = cookies().get("custom-session-cookie");
38-
if (!session) return
39-
40-
const client = new Client()
41-
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
42-
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID)
43-
44-
client.setSession(session.value)
45-
const account = new Account(client);
46-
47-
return await account.get()
48-
}
49-
`
22+
content: SnippetSvelteKit
5023
},
5124
{
5225
language: Platform.ClientWeb,
5326
platform: Framework.Astro,
54-
content: `import { Client, Account } from 'node-appwrite'
55-
56-
async function getLoggedInUser(context) {
57-
const session = cookies().get("custom-session-cookie");
58-
if (!session) return
59-
60-
const client = new Client()
61-
.setEndpoint(import.meta.env.PUBLIC_APPWRITE_ENDPOINT)
62-
.setProject(import.meta.env.PUBLIC_APPWRITE_PROJECT_ID)
63-
64-
client.setSession(session.value)
65-
const account = new Account(client);
66-
67-
return await account.get()
68-
}
69-
`
27+
content: SnippetAstro
7028
},
7129
{
7230
language: Platform.ClientWeb,
7331
platform: Framework.Nuxt3,
74-
content: `import { Client, Account } from 'node-appwrite'
75-
import { H3Event, getCookie } from 'h3'
76-
77-
async function getLoggedInUser(event) {
78-
const session = getCookie(event, "custom-session-cookie");
79-
if (!session) return
80-
81-
const client = new Client()
82-
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
83-
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID)
84-
85-
client.setSession(session.value)
86-
const account = new Account(client);
87-
88-
return await account.get()
89-
}`
32+
content: SnippetNuxt
9033
},
9134
{
9235
language: Platform.ClientWeb,
9336
platform: Framework.Remix,
94-
content: `import { Client, Account } from 'node-appwrite'
95-
import { createCookie } from '@remix-run/node'
96-
97-
export const customSessionCookie = createCookie("custom-session-cookie", {
98-
maxAge: 604800,
99-
})
100-
101-
async function getLoggedInUser(request) {
102-
const cookies = request.headers.get("Cookie")
103-
const session = await customSessionCookie.parse(cookies)
104-
if (!session) return
105-
106-
const client = new Client()
107-
.setEndpoint(process.env.PUBLIC_APPWRITE_ENDPOINT)
108-
.setProject(process.env.PUBLIC_APPWRITE_PROJECT_ID)
109-
110-
client.setSession(session.value)
111-
const account = new Account(client);
112-
113-
return await account.get()
114-
}`
37+
content: SnippetRemix
11538
}
11639
];
11740
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace DotNetRuntime;
2+
3+
using Appwrite;
4+
using Appwrite.Services;
5+
using Appwrite.Models;
6+
7+
public class Handler {
8+
9+
// This is your Appwrite function
10+
// It"s executed each time we get a request
11+
public async Task Main(RuntimeContext Context)
12+
{
13+
// Why not try the Appwrite SDK?
14+
//
15+
// Set project and set API key
16+
// var client = new Client()
17+
// .SetProject(Environment.GetEnvironmentVariable("APPWRITE_FUNCTION_PROJECT_ID"))
18+
// .SetKey(Context.Req.Headers["x-appwrite-key"]);
19+
20+
// You can log messages to the console
21+
Context.Log("Hello, Logs!");
22+
23+
// If something goes wrong, log an error
24+
Context.Error("Hello, Errors!");
25+
26+
// The 'Context.Req' object contains the request data
27+
if (Context.Req.Method == "GET") {
28+
// Send a response with the res object helpers
29+
// 'Context.Res.Text()' dispatches a string back to the client
30+
return Context.Res.Text("Hello, World!");
31+
}
32+
33+
// 'Context.Res.Json()' is a handy helper for sending JSON
34+
return Context.Res.Json(new Dictionary()
35+
{
36+
{ "motto", "Build like a team of hundreds_" },
37+
{ "learn", "https://appwrite.io/docs" },
38+
{ "connect", "https://appwrite.io/discord" },
39+
{ "getInspired", "https://builtwith.appwrite.io" },
40+
});
41+
}
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'dart:async';
2+
import 'package:dart_appwrite/dart_appwrite.dart';
3+
4+
// This is your Appwrite function
5+
// It's executed each time we get a request
6+
Future main(final context) async {
7+
// Why not try the Appwrite SDK?
8+
//
9+
// Set project and set API key
10+
// final client = Client()
11+
// .setProject(Platform.environment['APPWRITE_FUNCTION_PROJECT_ID'])
12+
// .setKey(context.req.headers['x-appwrite-key']);
13+
14+
15+
// You can log messages to the console
16+
context.log('Hello, Logs!');
17+
18+
// If something goes wrong, log an error
19+
context.error('Hello, Errors!');
20+
21+
// The 'req' object contains the request data
22+
if (context.req.method == 'GET') {
23+
// Send a response with the res object helpers
24+
// 'res.text()' dispatches a string back to the client
25+
return context.res.text('Hello, World!');
26+
}
27+
28+
// 'res.json()' is a handy helper for sending JSON
29+
return context.res.json({
30+
'motto': 'Build like a team of hundreds_',
31+
'learn': 'https://appwrite.io/docs',
32+
'connect': 'https://appwrite.io/discord',
33+
'getInspired': 'https://builtwith.appwrite.io',
34+
});
35+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Client } from "https://deno.land/x/[email protected]/mod.ts";
2+
3+
// This is your Appwrite function
4+
// It's executed each time we get a request
5+
export default ({ req, res, log, error }: any) => {
6+
// Why not try the Appwrite SDK?
7+
//
8+
// Set project and set API key
9+
// const client = new Client()
10+
// .setProject(Deno.env.get("APPWRITE_FUNCTION_PROJECT_ID") || "")
11+
// .setKey(req.headers["x-appwrite-key"] || "");
12+
13+
// You can log messages to the console
14+
log("Hello, Logs!");
15+
16+
// If something goes wrong, log an error
17+
error("Hello, Errors!");
18+
19+
// The 'req' object contains the request data
20+
if (req.method === "GET") {
21+
// Send a response with the res object helpers
22+
// 'res.text()' dispatches a string back to the client
23+
return res.text("Hello, World!");
24+
}
25+
26+
// 'res.json()' is a handy helper for sending JSON
27+
return res.json({
28+
motto: "Build like a team of hundreds_",
29+
learn: "https://appwrite.io/docs",
30+
connect: "https://appwrite.io/discord",
31+
getInspired: "https://builtwith.appwrite.io",
32+
});
33+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package handler
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"github.com/appwrite/sdk-for-go/appwrite"
7+
"github.com/open-runtimes/types-for-go/v4/openruntimes"
8+
)
9+
10+
type Response struct {
11+
Motto string `json:"motto"`
12+
Learn string `json:"learn"`
13+
Connect string `json:"connect"`
14+
GetInspired string `json:"getInspired"`
15+
}
16+
17+
func Main(Context openruntimes.Context) openruntimes.Response {
18+
// This is your Appwrite function
19+
// It's executed each time we get a request service
20+
var _ = appwrite.NewClient(
21+
appwrite.WithProject(os.Getenv("APPWRITE_FUNCTION_PROJECT_ID")),
22+
appwrite.WithKey(Context.Req.Headers["x-appwrite-key"]),
23+
)
24+
25+
// You can log messages to the console
26+
fmt.Println("Hello, Logs!")
27+
28+
fmt.Fprintln(os.Stderr, "Error:", "Hello, Errors!")
29+
30+
// The 'Context.Req' object contains the request data
31+
if Context.Req.Method == "GET" {
32+
// Send a response with the Context.Res object helpers
33+
// 'Context.Res.Text()' dispatches a string back to the client
34+
return Context.Res.Text("Hello, World!")
35+
}
36+
37+
// 'res.json()' is a handy helper for sending JSON
38+
return Context.Res.Json(
39+
Response{
40+
Motto: "Build like a team of hundreds_",
41+
Learn: "https://appwrite.io/docs",
42+
Connect: "https://appwrite.io/discord",
43+
GetInspired: "https://builtwith.appwrite.io",
44+
})
45+
}

0 commit comments

Comments
 (0)