Skip to content

Commit

Permalink
Update readme and clean up backend code
Browse files Browse the repository at this point in the history
  • Loading branch information
xsarahyu committed Apr 15, 2024
1 parent 618c109 commit e8a470e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# AI Messenger
# AI Chatbot

## 👋 Introduction
Welcome to the **AI Chatbot**! This application allows users to interact with an AI-powered chatbot using natural language. Built with React and Node, it offers a seamless experience for engaging in conversations with an intelligent virtual assistant.
7 changes: 3 additions & 4 deletions backend/passport_conf.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dotenv/config'
import passport from 'passport';
import { Strategy } from 'passport-google-oauth20';

import passport from 'passport'
import { Strategy } from 'passport-google-oauth20'

const conf = {
clientID: process.env.clientID,
Expand All @@ -14,4 +13,4 @@ passport.use(new Strategy(conf, (req, aToken, rToken, pro, done) => done(null, p

passport.serializeUser((user, done) => done(null, user))

passport.deserializeUser((user, done) => done(null, user))
passport.deserializeUser((user, done) => done(null, user))
44 changes: 21 additions & 23 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,33 @@ import './passport_conf.js'
import express from 'express'
import passport from 'passport'
import session from 'express-session'

import { GoogleGenerativeAI } from "@google/generative-ai"

// Access your API key as an environment variable (see "Set up your API key" above)
const genAI = new GoogleGenerativeAI(process.env.geminiKey);

const genAI = new GoogleGenerativeAI(process.env.geminiKey)

const app = express();
const app = express()
const PORT = process.env.PORT

app.use(cors({credentials: true, origin: true}))
app.use(express.json())

// express session
app.use(session({
secret: process.env.secret,
resave: false,
saveUninitialized: false
}));
}))


app.use(passport.initialize());
app.use(passport.session());
app.use(passport.initialize())
app.use(passport.session())

// Middleware used in protected routes to check if the user has been authenticated
const isLoggedIn = (req, res, next) => {
if (req.user) {
next();
next()
} else {
res.sendStatus(401);
res.sendStatus(401)
}
}

Expand All @@ -43,7 +42,7 @@ app.get("/home", (req, res) => {
// Google Auth consent screen route
app.get('/google',
passport.authenticate('google', { scope: ['email', 'profile'] }
));
))

// Call back route
app.get('/google/callback',
Expand All @@ -52,17 +51,17 @@ app.get('/google/callback',
}), (req, res) => {
res.redirect('http://localhost:5173/messages')
}
);
)

// failed route if the authentication fails
// Failed route if the authentication fails
app.get("/failed", (req, res) => {
console.log('User is not authenticated');
console.log('User is not authenticated')
res.send("Failed")
})

// Success route if the authentication is successful
app.get("/success",isLoggedIn, (req, res) => {
console.log('You are logged in');
console.log('You are logged in')
res.send(`Welcome ${req.user.displayName}, your userID is ${req.user.id}`)
})

Expand All @@ -74,15 +73,15 @@ app.get("/userdata", (req, res) => {
app.get("/logout", (req, res) => {
req.session.destroy((err) => {
if (err) {
console.log('Error while destroying session:', err);
console.log('Error while destroying session:', err)
} else {
req.logout(() => {
console.log('You are logged out');
res.redirect('/home');
});
console.log('You are logged out')
res.redirect('/home')
})
}
});
});
})
})

const talkToAi = async (text) => {
const model = genAI.getGenerativeModel({ model: "gemini-pro"})
Expand All @@ -97,5 +96,4 @@ app.post("/api", isLoggedIn, async (req, res) => {
res.json({message: resp})
})

app.listen(PORT, () => console.log("server running on port" + PORT))

app.listen(PORT, () => console.log("server running on port" + PORT))

0 comments on commit e8a470e

Please sign in to comment.