api and shadcn
This commit is contained in:
@@ -1,13 +1,87 @@
|
||||
import { NextResponse, NextRequest } from 'next/server';
|
||||
import { TwitterApi } from 'twitter-api-v2';
|
||||
import config from '@/env';
|
||||
import { PostRequest } from '@/platform';
|
||||
import config from '@/app/api/env';
|
||||
import { PostRequest } from '@/utils';
|
||||
import { inspect } from 'util';
|
||||
import database from '../database';
|
||||
import { generateUrlSafeString } from '@/utils';
|
||||
|
||||
// Account creation
|
||||
// Email doesn't exist
|
||||
// Password is between 6-20 characters
|
||||
export async function POST(req: NextRequest) {
|
||||
const { email, password } = await req.json();
|
||||
|
||||
if (typeof email !== 'string' || typeof password !== 'string')
|
||||
return NextResponse.json(
|
||||
{
|
||||
message:
|
||||
"Invalid structure, body requires 'email: string' and 'password: string'",
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
|
||||
if (await database.users.countDocuments({ email }))
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'Email already registered',
|
||||
},
|
||||
{ status: 409 }
|
||||
);
|
||||
|
||||
await database.users.insertOne({ email, password });
|
||||
|
||||
const response = NextResponse.json(
|
||||
{
|
||||
message: 'ok',
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
|
||||
response.cookies.set('session_id', generateUrlSafeString(16));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Account login
|
||||
// Email exists
|
||||
// Password is valid
|
||||
export async function GET(req: NextRequest) {
|
||||
const { email, password } = await req.json();
|
||||
|
||||
if (typeof email !== 'string' || typeof password !== 'string')
|
||||
return NextResponse.json(
|
||||
{
|
||||
message:
|
||||
"Invalid structure, body requires 'email: string' and 'password: string'",
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
|
||||
const user = await database.users.findOne({ email });
|
||||
|
||||
if (!user)
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'Invalid email',
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
|
||||
if (user.password !== password)
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'Invalid password',
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
|
||||
const sessionId = (await database.sessions.insertOne({ email })).insertedId;
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
message: 'ok, feature not available yet',
|
||||
message: 'ok',
|
||||
sessionId,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { MongoClient } from 'mongodb';
|
||||
import config from './env';
|
||||
import { generateUrlSafeString, Session, User } from '@/utils';
|
||||
|
||||
const client = new MongoClient(config.mongoUrl);
|
||||
|
||||
const db = client.db('klayrion');
|
||||
|
||||
export default {
|
||||
users: db.collection<User>('users'),
|
||||
sessions: db.collection<Session>('sessions'),
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
function error(name: string): string {
|
||||
throw `'${name}' not found in '.env'`;
|
||||
}
|
||||
|
||||
const config = {
|
||||
twitter: {
|
||||
key: process.env.TWITTER_KEY || error('TWITTER_KEY'),
|
||||
secret: process.env.TWITTER_SECRET || error('TWITTER_SECRET'),
|
||||
},
|
||||
|
||||
mongoUrl: process.env.MONGO_URL || error('MONGO_URL'),
|
||||
};
|
||||
|
||||
export default config;
|
||||
@@ -1,7 +1,7 @@
|
||||
import { NextResponse, NextRequest } from 'next/server';
|
||||
import { TwitterApi } from 'twitter-api-v2';
|
||||
import config from '@/env';
|
||||
import { PostRequest } from '@/platform';
|
||||
import config from '@/app/api/env';
|
||||
import { PostRequest } from '@/utils';
|
||||
import { inspect } from 'util';
|
||||
|
||||
export async function POST(req: NextRequest) {
|
||||
|
||||
Reference in New Issue
Block a user