jwt 스터디 1차
Sever/Node.js 2019. 1. 2. 20:55이해하는데 시간이 좀 걸릴듯 일단 예제는 따라서 해봤음...
알아야 할게 한두가지가 아닌듯;
첨부파일 :
npm install 할것
참고
https://victorydntmd.tistory.com/116
https://www.tutorialspoint.com/expressjs/expressjs_routing.htm
https://hyunseob.github.io/2016/03/27/usage-of-sequelize-js/
https://www.npmjs.com/package/mysql2
https://hyunseob.github.io/2016/03/27/usage-of-sequelize-js/
유투브 jwt관련
https://www.youtube.com/results?search_query=jwt
app.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | //https://www.tutorialspoint.com/expressjs/expressjs_middleware.htm //https://victorydntmd.tistory.com/116 const express = require('express'); const test = require('./test'); const jwt = require('jsonwebtoken'); const secretObj = require('./jwt'); const app = express(); app.get('/', (req, res)=>{ let token = jwt.sign({ username: 'janedoe' }, secretObj.secret, { expiresIn: '5m' }) test.find('janedoe', (user)=>{ console.log(user.username); if(user.username === "janedoe"){ res.cookie("user", token); res.json({ token: token }) } }); }); app.listen(3000); | cs |
jwt.js
1 2 3 | let jwtObj = {}; jwtObj.secret = 'apple'; module.exports = jwtObj; | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | //http://docs.sequelizejs.com/ const Sequelize = require('sequelize'); const sequelize = new Sequelize('test', 'root', 'root', { host: '192.168.0.133', dialect: 'mysql', pool: { max: 5, min: 0, acquire: 30000, idle: 10000 }, // SQLite only //storage: 'path/to/database.sqlite', // http://docs.sequelizejs.com/manual/tutorial/querying.html#operators operatorsAliases: false }); const User = sequelize.define('user', { username: Sequelize.STRING, birthday: Sequelize.DATE }); sequelize.sync() .then(() => User.create({ username: 'janedoe', birthday: new Date(1980, 6, 20) })) .then(jane => { //console.log(jane.toJSON()); }); module.exports = { find: (name, callback)=>{ User.findOne({ where:{ username: name } }).then((user)=>{ callback(user); }); } }; | cs |
sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | show databases; use test; #https://dev.mysql.com/doc/refman/8.0/en/creating-tables.html # id (int) , email (varchar) , pwd (varchar), createdAt(date), updatedAt(date) create table users ( id int not null, email varchar(255) not null, pwd varchar(255) not null, createAt date not null, updatedAt date, primary key (id) ); desc users; insert into users(id, email, pwd, createAt) values(1, 'foo@example.com', '1234', sysdate()); insert into users(id, email, pwd, createAt) values(1, 'foo@example.com', '1234', now()); #http://intomysql.blogspot.com/2010/12/sysdate-now.html #sysdate 와 now의 차이점 #SYSDATE() 함수는 트랜잭션이나 쿼리 단위에 전혀 관계 없이, 그 함수가 실행되는 시점의 시각을 리턴해주지만, #NOW()는 하나의 쿼리 단위로 동일한 값을 리턴하게 된다. #affected, 1 warning(s): 1265 Data truncated for column 'createAt' at row 1 select now(); select sysdate(); select * from users; delete from users where id = 1; desc users; #http://majesty76.tistory.com/27 #구조 변경 alter table users modify column updatedAt varchar(255); #컬럼 명 변경 alter table users change createAt createdAt varchar(255) not null; select * from users; insert into users(id, email, pwd, createdAt) values(2, 'goo@example.com', '1111', now()); update users set createdAt = now() where id=1; select * from users; drop table users; show tables; desc users; select * from users; delete from users; | cs |
'Sever > Node.js' 카테고리의 다른 글
JWT를 구현하면서 마주치게 되는 고민들 (0) | 2019.01.03 |
---|---|
jwt 스터디 2차 (0) | 2019.01.03 |
jwt (0) | 2019.01.01 |
Node.js Passport login (0) | 2018.12.20 |
Study : Express, RESTful API (0) | 2018.10.24 |