Skip to content

Instantly share code, notes, and snippets.

View reactFormValidation.js
const MIN_PASSWORD_LENGTH = 6;
const [data, setData] = React.useState({
name: '',
email: '',
password: '',
confirmPassword:''
})
const errors = {
name: '',
email: '',
View route-gards.js
<Switch>
{!user && <Redirect exact from='/' to='/auth' />}
// you can only place the Redirect component inside the Switch Component if you are using the from to sytax
{user && <Redirect exact from='/' to='/dashboard'/>}
{user && <Redirect from='/auth' to='/dashboard'/>}
<Route path='/auth' component={AuthPage}/>
<Route path='/landing' component={LandingPage} />
<Route path='/' component={ProjectPage}/>
</Switch>
View authRuotes.js
// sign up route
Router.post('/signUp',cors(), async (req, res) => {
const {email, password, name, imgUrl} = req.body
if(!email || !password || !name) return res.status(400).json({msg: 'all fields are required'})
try {
const user = await User.findOne({email})
if(user) return res.status(400).json({msg: 'email already exists'})
View shuffle.js
//One could (or should) use it as a protoype from Array:
//From ChristopheD:
Array.prototype.shuffle = function() {
var i = this.length, j, temp;
if ( i == 0 ) return this;
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
temp = this[i];
@moaoa
moaoa / socket-cheatsheet.js
Created Oct 13, 2020 — forked from alexpchin/socket-cheatsheet.js
A quick cheatsheet for socket.io
View socket-cheatsheet.js
// sending to sender-client only
socket.emit('message', "this is a test");
// sending to all clients, include sender
io.emit('message', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('message', "this is a test");
// sending to all clients in 'game' room(channel) except sender
View push-multiple-branches-to-github
git push origin --all
// will push all branches to github
View $in-mongoose.js
// With a JSON doc
Person.
find({
occupation: /host/,
'name.last': 'Ghost',
age: { $gt: 17, $lt: 66 },
likes: { $in: ['vaporizing', 'talking'] } // see if the value of likes included in ['vaporizing, 'talking'] array
// syntax {property: {$in: array...}}
}).
limit(10).
View jwt.js
// gnerate token function
const jwt = require('jsonwebtoken');
const jwtSecret = process.env.JWT_SECRET;
module.exports = (obj) => {
return jwt.sign(obj, jwtSecret, {
expiresIn: '1day',
});
};
// ==================
View axios.js
const axios = require('axios')
// queryParams
axios.get(url, {
params:{
key: value
}
}
)
// output