-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
3,111 additions
and
159 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
import { env } from "$env/dynamic/private"; | ||
export const TABLE_NAME = env.BOOKLIST_DYNAMO; | ||
|
||
export const getGetPacket = (pk, sk, rest = {}) => ({ TableName: TABLE_NAME, Key: { pk, sk }, ...rest }); | ||
export const getQueryPacket = (keyExpression, rest = {}) => ({ | ||
TableName: TABLE_NAME, | ||
KeyConditionExpression: keyExpression, | ||
...rest | ||
}); | ||
export const getPutPacket = (obj, rest = {}) => ({ TableName: TABLE_NAME, Item: obj, ...rest }); | ||
export const getUpdatePacket = (pk, sk, rest) => ({ TableName: TABLE_NAME, Key: { pk, sk }, ...rest }); | ||
|
||
const dynamo = new AWS.DynamoDB.DocumentClient({ | ||
region: "us-east-1" | ||
}); | ||
|
||
export { dynamo }; | ||
|
||
export const db = { | ||
async put(packet) { | ||
await dynamo.put(packet).promise(); | ||
}, | ||
|
||
async get(packet) { | ||
let result = await dynamo.get(packet).promise(); | ||
return result.Item || null; | ||
}, | ||
|
||
async queryOne(packet) { | ||
let res = await dynamo.query(packet).promise(); | ||
|
||
if (!res || !res.Items || !res.Items[0]) { | ||
return null; | ||
} | ||
|
||
return res.Items[0]; | ||
}, | ||
|
||
async pagedQuery(packet) { | ||
let result = await dynamo.query(packet).promise(); | ||
|
||
return { items: result.Items || null, lastEvaluatedKey: result.LastEvaluatedKey }; | ||
}, | ||
|
||
async update(packet) { | ||
return dynamo.update(packet).promise(); | ||
}, | ||
|
||
async transactWrite(packet) { | ||
return dynamo.transactWrite(packet).promise(); | ||
}, | ||
|
||
async deleteItem(pk, sk) { | ||
return dynamo.delete({ TableName: TABLE_NAME, Key: { pk, sk } }).promise(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import md5 from "blueimp-md5"; | ||
import { env } from "$env/dynamic/private"; | ||
|
||
import { db, getQueryPacket, getPutPacket } from "./dynamoHelpers"; | ||
|
||
const getUserAliasKey = (userId: string) => `UserAlias#${userId}`; | ||
|
||
const salt = env.SALT; | ||
|
||
export async function lookupUser(email: string, password: string) { | ||
email = email.toLowerCase(); | ||
password = saltAndHashPassword(password); | ||
const userKey = `User#${email}`; | ||
|
||
try { | ||
let userFound = await db.queryOne( | ||
getQueryPacket(` pk = :userKey AND sk = :userKey `, { | ||
ExpressionAttributeValues: { ":password": password, ":userKey": userKey, ":true": true }, | ||
FilterExpression: ` password = :password AND awaitingActivation <> :true ` | ||
}) | ||
); | ||
|
||
if (!userFound) { | ||
return null; | ||
} | ||
|
||
const id = userFound.userId; | ||
|
||
return { | ||
id | ||
}; | ||
} catch (loginErr) { | ||
console.log("Login error", loginErr); | ||
return null; | ||
} | ||
} | ||
|
||
export async function syncUser(newId: string, legacyId: string) { | ||
const userSync = { | ||
pk: getUserAliasKey(newId), | ||
sk: legacyId | ||
}; | ||
|
||
db.put(getPutPacket(userSync)); | ||
} | ||
|
||
export async function getUserSync(userId: string) { | ||
const key = getUserAliasKey(userId); | ||
|
||
try { | ||
const syncEntry = await db.queryOne( | ||
getQueryPacket(` pk = :key `, { | ||
ExpressionAttributeValues: { ":key": key } | ||
}) | ||
); | ||
|
||
return syncEntry; | ||
} catch (er) { | ||
return null; | ||
} | ||
} | ||
|
||
function saltAndHashPassword(password: string) { | ||
return md5(`${salt}${password}${salt}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,78 @@ | ||
import { sequence } from "@sveltejs/kit/hooks"; | ||
import SvelteKitAuth from "@auth/sveltekit"; | ||
import GoogleProvider from "@auth/core/providers/google"; | ||
import { GOOGLE_AUTH_CLIENT_ID, GOOGLE_AUTH_SECRET, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, DYNAMO_AUTH_TABLE } from "$env/static/private"; | ||
|
||
import { DynamoDB, type DynamoDBClientConfig } from "@aws-sdk/client-dynamodb"; | ||
import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb"; | ||
import { DynamoDBAdapter } from "@next-auth/dynamodb-adapter"; | ||
import { getUserSync } from "$data/legacyUser"; | ||
|
||
const dynamoConfig: DynamoDBClientConfig = { | ||
credentials: { | ||
accessKeyId: AWS_ACCESS_KEY_ID, | ||
secretAccessKey: AWS_SECRET_ACCESS_KEY | ||
}, | ||
|
||
region: "us-east-1" | ||
}; | ||
|
||
const client = DynamoDBDocument.from(new DynamoDB(dynamoConfig), { | ||
marshallOptions: { | ||
convertEmptyValues: true, | ||
removeUndefinedValues: true, | ||
convertClassInstanceToMap: true | ||
} | ||
}); | ||
|
||
const auth = SvelteKitAuth({ | ||
providers: [ | ||
GoogleProvider({ | ||
clientId: GOOGLE_AUTH_CLIENT_ID, | ||
clientSecret: GOOGLE_AUTH_SECRET | ||
}) | ||
], | ||
session: { | ||
maxAge: 60 * 60 * 24 * 365, | ||
strategy: "jwt" | ||
}, | ||
|
||
secret: process.env.NEXTAUTH_SECRET, | ||
|
||
// adapter: DynamoDBAdapter(client, { tableName: DYNAMO_AUTH_TABLE }) as any | ||
|
||
callbacks: { | ||
async signIn({ account }) { | ||
(account as any).overridden = "HELLO"; | ||
|
||
if (account == null) { | ||
return false; | ||
} | ||
|
||
const userSync = await getUserSync(account.providerAccountId); | ||
console.log({ userSync }); | ||
|
||
return true; | ||
}, | ||
async jwt({ token, account }) { | ||
token.userId ??= account?.providerAccountId; | ||
return token; | ||
}, | ||
async session({ session, user, token }) { | ||
(session as any).userId = token.userId; | ||
return session; | ||
} | ||
} | ||
}); | ||
|
||
const PRELOAD = new Set(["font", "js", "css"]); | ||
|
||
export async function handle({ event, resolve }: any) { | ||
export async function preload({ event, resolve }: any) { | ||
const response = await resolve(event, { | ||
preload: ({ type }: any) => PRELOAD.has(type) | ||
}); | ||
|
||
return response; | ||
} | ||
|
||
export const handle = sequence(preload, auth); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.