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

added authentication routes #2

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ module.exports = {
'no-console': 'off',
'import/prefer-default-export': 'off',
'@typescript-eslint/no-unused-vars': 'warn',
'prefer-spread': ['off']
},
};
165 changes: 165 additions & 0 deletions commands.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
drop table if exists companies;
drop table if exists apps;
drop table if exists api_keys;
drop table if exists users;
drop table if exists channels;
drop table if exists messages;
drop table if exists user_app;
drop table if exists company_app;
drop table if exists api_key_app;
drop table if exists user_channel;
drop table if exists message_channel;
drop table if exists message_user;
drop table if exists developers;
drop table if exists company_developer;
drop table if exists developer_app;
drop table if exists api_key_developer;

CREATE TABLE companies (
id UUID PRIMARY KEY default uuid_generate_v4(),
name VARCHAR(255) NOT NULL,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
updated_at timestamp with time zone default timezone('utc'::text, now()) not null
);

CREATE TABLE apps (
id UUID PRIMARY KEY default uuid_generate_v4(),
name VARCHAR(255) NOT NULL,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
updated_at timestamp with time zone default timezone('utc'::text, now()) not null
);

CREATE TABLE api_keys (
id UUID PRIMARY KEY default uuid_generate_v4(),
name VARCHAR(255) NOT NULL,
api_key VARCHAR(255) NOT NULL,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
updated_at timestamp with time zone default timezone('utc'::text, now()) not null
);

CREATE TABLE developers (
id UUID PRIMARY KEY default uuid_generate_v4(),
username VARCHAR(255) NOT NULL,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
updated_at timestamp with time zone default timezone('utc'::text, now()) not null
);

CREATE TABLE users (
id UUID PRIMARY KEY default uuid_generate_v4(),
username VARCHAR(255) NOT NULL,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
updated_at timestamp with time zone default timezone('utc'::text, now()) not null
);

CREATE TABLE channels (
id UUID PRIMARY KEY default uuid_generate_v4(),
name VARCHAR(255) NOT NULL,
owner_user_id UUID REFERENCES users(id) on delete set null,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
updated_at timestamp with time zone default timezone('utc'::text, now()) not null
);

CREATE TABLE messages (
id UUID PRIMARY KEY default uuid_generate_v4(),
message TEXT NOT NULL,
created_at timestamp with time zone default timezone('utc'::text, now()) not null,
updated_at timestamp with time zone default timezone('utc'::text, now()) not null
);

CREATE TABLE company_developer (
id UUID PRIMARY KEY default uuid_generate_v4(),
company_id UUID REFERENCES companies(id) on delete cascade,
developer_owner_id UUID REFERENCES developers(id) on delete cascade
);

CREATE TABLE developer_app (
id UUID PRIMARY KEY default uuid_generate_v4(),
developer_id UUID REFERENCES developers(id) on delete cascade,
app_id UUID REFERENCES apps(id) on delete cascade
);

CREATE TABLE company_app (
id UUID PRIMARY KEY default uuid_generate_v4(),
company_id UUID REFERENCES companies(id) on delete cascade,
app_id UUID REFERENCES apps(id) on delete cascade
);
CREATE TABLE channel_app (
id UUID PRIMARY KEY default uuid_generate_v4(),
channel_id UUID REFERENCES channels(id) on delete cascade,
app_id UUID REFERENCES apps(id) on delete cascade
);

CREATE TABLE api_key_app (
id UUID PRIMARY KEY default uuid_generate_v4(),
api_key_id UUID REFERENCES api_keys(id) on delete set null,
app_id UUID REFERENCES apps(id) on delete cascade
);

CREATE TABLE api_key_developer (
id UUID PRIMARY KEY default uuid_generate_v4(),
api_key_id UUID REFERENCES api_keys(id) on delete set null,
developer_id UUID REFERENCES developers(id) on delete cascade
);

CREATE TABLE user_app (
id UUID PRIMARY KEY default uuid_generate_v4(),
user_id UUID REFERENCES users(id) on delete set null,
app_id UUID REFERENCES apps(id) on delete cascade
);

CREATE TABLE user_channel (
id UUID PRIMARY KEY default uuid_generate_v4(),
user_id UUID REFERENCES users(id) on delete set null,
channel_id UUID REFERENCES channels(id) on delete cascade
);

CREATE TABLE message_channel (
id UUID PRIMARY KEY default uuid_generate_v4(),
message_id UUID REFERENCES messages(id) on delete set null,
channel_id UUID REFERENCES channels(id) on delete cascade
);

CREATE TABLE message_user (
id UUID PRIMARY KEY default uuid_generate_v4(),
message_id UUID REFERENCES messages(id) on delete set null,
user_id UUID REFERENCES users(id) on delete cascade
);


CREATE OR REPLACE FUNCTION trigger_set_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = timezone('utc'::text, now());
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER set_timestamp
BEFORE UPDATE ON apps
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

CREATE TRIGGER set_timestamp
BEFORE UPDATE ON api_keys
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

CREATE TRIGGER set_timestamp
BEFORE UPDATE ON companies
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

CREATE TRIGGER set_timestamp
BEFORE UPDATE ON users
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

CREATE TRIGGER set_timestamp
BEFORE UPDATE ON channels
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();

CREATE TRIGGER set_timestamp
BEFORE UPDATE ON messages
FOR EACH ROW
EXECUTE PROCEDURE trigger_set_timestamp();
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
"homepage": "https://github.com/nsmet/supabase-node-chat-backend#readme",
"dependencies": {
"@supabase/supabase-js": "^2.4.1",
"@types/jsonwebtoken": "^9.0.1",
"body-parser": "^1.20.1",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"socket.io": "^4.5.4"
},
"devDependencies": {
Expand Down
152 changes: 152 additions & 0 deletions src/controllers/apps.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
import { Response } from "express"
import {
TypedRequestBody, TypedRequestQueryWithParams,
} from '../types';
import supabase from "../utils/supabase"

export const createNewApp = async (req:TypedRequestBody<{name:string,developer_id:string}>, res:Response) => {
const name = req.body.name
const devID = req.body.developer_id
// TODO - need to get companyID myself from user.
const companyID = "0d871b21-03d6-4e75-873a-480d5ae097b9"
const appID = await addApp(name)
if(!appID) return res.sendStatus(500)
const companyAppID = await addCompanyApp(companyID,appID)
if(!companyAppID) return res.sendStatus(500)
const developerAppID = await addDeveloperApp(appID,devID)
if(!developerAppID) return res.sendStatus(500)
return res.send(appID)
}

async function addApp(name:string):Promise<string | null>{
try{
const { data, error } = await supabase
.from('apps')
.upsert({
name:name
})
.select()
if (error) {
console.log(error)
return null;
} else {
return data[0].id
}
}
catch(err){
console.log(err)
return null
}
}
async function addCompanyApp(companyID:string,appID:string):Promise<string | null>{
try{
const { data, error } = await supabase
.from('company_app')
.upsert({
company_id:companyID,
app_id:appID
})
.select()
if (error) {
console.log(error)
return null;
} else {
return data[0].id
}
}catch(err){
console.log(err)
return null
}
}
async function addDeveloperApp(appID:string,devID:string):Promise<string | null>{
try{
const { data, error } = await supabase
.from('developer_app')
.upsert({
developer_id:devID,
app_id:appID
})
.select()
if (error) {
console.log(error)
return null;
} else {
return data[0].id
}
}catch(err){
console.log(err)
return null
}
}


export const deleteAppByID = async (req:TypedRequestQueryWithParams<{app_id:string}>, res:Response) => {
const appID = req.params.app_id
const deletedChannelApp = await removeChannelApp(appID)
if (!deletedChannelApp) return res.sendStatus(500)
const deletedCompanyApp = await removeCompanyApp(appID)
if (!deletedCompanyApp) return res.sendStatus(500)
const deletedDeveloperApp = await removeDeveloperApp(appID)
if (!deletedDeveloperApp) return res.sendStatus(500)
const deletedApp = await removeApp(appID)
if (!deletedApp) return res.sendStatus(500)
return res.send(deletedApp)
}

const removeApp = async function(appID:string) {
const {error,data} = await supabase
.from('apps')
.delete()
.eq('id', appID)
.select()
if (error){
console.log(error)
return null
}else {
console.log({data})
return data
}
}
const removeChannelApp = async function(appID:string) {
const {error,data} = await supabase
.from('channel_app')
.delete()
.eq('app_id', appID)
.select()
if (error){
console.log(error)
return null
}else {
console.log({data})
return data
}
}
const removeCompanyApp = async function(appID:string) {
const {error,data} = await supabase
.from('company_app')
.delete()
.eq('app_id', appID)
.select()
if (error){
console.log(error)
return null
}else {
console.log({data})
return data
}
}
const removeDeveloperApp = async function(appID:string) {
const {error,data} = await supabase
.from('developer_app')
.delete()
.eq('app_id', appID)
.select()
if (error){
console.log(error)
return null
}else {
console.log({data})
return data
}
}

Loading