Professional Documents
Culture Documents
web-appvdbdh
web-appvdbdh
Server
npm init
"dependencies": {
"apollo-server-express": "^3.13.0",
"axios": "^1.7.2",
"cors": "^2.8.5",
"express": "^4.19.2",
"express-session": "^1.18.0",
"graphql": "^16.9.0",
"graphql-tools": "^9.0.1",
"mongodb": "^6.8.0",
"mongoose": "^8.4.4",
"nodemon": "^3.1.4",
"superagent": "^9.0.2"
}
/users
1) Create a User / users/create
2) Update Password /users/changePass
3) Login a User. /users/login
Index.js
// CORS middleware
app.use(cors());
app.use(express.json())//parsing
mongoose.connect(url,{useNewUrlParser:true,
useUnifiedTopology:true}).then(()=>{console.log('DB connected')})
.catch((err)=>{console.log(err)});
app.use(‘/users',userApiFromRouter); //api
schema.js
//schema.js
//create a schema using GQL
const { gql } = require('apollo-server-express');
const typeDefs = gql`
type User {
id:ID !,
name:String!,
email:String!,
password:String!
}
type Query {
getUser(id:ID!):User
getUsers:[User]
}
input createUserInput{
name:String!,
email:String!,
password:String!
}
type Mutation{
createUser(input:createUserInput!):User
changePass(id:ID!,password:String!):User
}
`;
module.exports= typeDefs;//export out
resolvers.js
//resolvers.js
const User = require('./model/userSchema');//parent
const resolvers ={
Query:{
getUser: async(_,{id})=>{return await User.findById(id);},
getUsers: async()=>{return await User.find();},
},
Mutation:{
createUser: async (_,{input})=>{
const {name,email,password}= input;
try{
if(!name || !email || !password){
throw new Error("Please enter all the
fields");
}
const newUser = new User({name,email,password});
return await newUser.save();
}catch(err){ throw Error(`Error Creating User: ${err}
`)}
},
changePass:async(_,{id,password})=>{
try{
},
},
User:{
email:(parent)=> parent.email || '',
name:(parent)=> parent.name || '',
password:(parent)=> parent.password || ''
},
};module.exports=resolvers;//export resolvers
userRoute.js
router.put('/changepass/:id', async(req,res)=>{
const id = req.params.id;
const {password}= req.body;
try{
const {data,error} = await server.executeOperation({
query:gql`mutation{
changePass(id:"${id}",password:"${password}"){
id
name
password
}
} `
});
if(error){ return res.status(500).send({message:error})};
res.status(201).send({message:data});
}catch(err){res.status(500).send({message:err})};
})
module.exports =router
Client
Refer Document
https://tailwindcss.com/docs/guides/create-react-app
index.js
</div>
);
Register.js
try {
const response = await axios.post("http://
localhost:3001/users/create", formData);
console.log("Form data submitted successfully!",
response.data);
} catch (error) {
console.error("Error submitting form:", error);
// Handle error, e.g., show error message to user
}
};
return (
<form className="max-w-sm mx-auto py-5"
onSubmit={handleSubmit}>
<div className="mb-5">
<label htmlFor="name" className="block mb-2 text-
sm font-medium text-gray-900 dark:text-white">Your Name</label>
<input
type="text"
id="name"
className="bg-gray-50 border border-gray-300
text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-
blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600
dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500
dark:focus:border-blue-500"
value={formData.name}
onChange={handleChange}
/>
</div>
<div className="mb-5">
<label htmlFor="email" className="block mb-2 text-
sm font-medium text-gray-900 dark:text-white">Your email</label>
<input
type="email"
id="email"
className="bg-gray-50 border border-gray-300
text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-
blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600
dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500
dark:focus:border-blue-500"
placeholder="name@flowbite.com"
required
value={formData.email}
onChange={handleChange}
/>
</div>
<div className="mb-5">
<label htmlFor="password" className="block mb-2
text-sm font-medium text-gray-900 dark:text-white">Your password</
label>
<input
type="password"
id="password"
className="bg-gray-50 border border-gray-300
text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-
blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600
dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500
dark:focus:border-blue-500"
value={formData.password}
onChange={handleChange}
required
/>
</div>
<button
type="submit"
className="text-white bg-blue-700 hover:bg-
blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-
medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center
dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
>
Submit
</button>
</form>
);
}
Navbar.js
<li>
<Link to='/about'> About</Link>
</li>
<li>
<Link to='/register'> Register</Link>
</li>
<li>
<Link to='/login'>Login</Link>
</li>
</ul>
</div>
</div>
</nav>
</div>
)
}
Login Auth
Schema.js(Server )
getUser(email:String!):[User]
Resolvers.js(Server )
userSchema.js(Server )
try {
const response = await axios.post("http://
localhost:3001/users/login", formData);
console.log("Login successful!", response.data);
alert(response.data);
} catch (error) {
console.error("Error logging in:", error);
}
};
return (
<form className="max-w-sm mx-auto py-5"
onSubmit={handleSubmit}>
<div className="mb-5">
<label htmlFor="email" className="block mb-2 text-
sm font-medium text-gray-900 dark:text-white">Your email</label>
<input
type="email"
id="email"
className="bg-gray-50 border border-gray-300
text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-
blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600
dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500
dark:focus:border-blue-500"
placeholder="name@flowbite.com"
required
value={formData.email}
onChange={handleChange}
/>
</div>
<div className="mb-5">
<label htmlFor="password" className="block mb-2
text-sm font-medium text-gray-900 dark:text-white">Your password</
label>
<input
type="password"
id="password"
className="bg-gray-50 border border-gray-300
text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-
blue-500 block w-full p-2.5 dark:bg-gray-700 dark:border-gray-600
dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500
dark:focus:border-blue-500"
value={formData.password}
onChange={handleChange}
required
/>
</div>
<button
type="submit"
className="text-white bg-blue-700 hover:bg-
blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-
medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center
dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"
>
Login
</button>
</form>
);
}
navbar.js (Client Component)