Python/Flask

    [미니터] 인증 - sign-up, login

    1. 회원가입 시 비밀번호 암호화 import bcrypt @app.route("/sign-up", methods=['GET','POST']) def sign_up(): new_user = request.json new_user['password'] = bcrypt.hashpw( new_user['pasword'].encode('UTF_8'), bcrypt.gensalt() ) new_user_id = insert_user(new_user) new_user = get_user(new_user_id) return jsonify(new_user) Line 6-8 : hashpw() 함수를 사용하여 사용자의 비밀번호 UTF-8 엔코딩으로 byte로 변환 후 salting을 추가하여 암호화 한다. 2. 로그인 ..

    [미니터] 인증

    1. 인증 : 사용자의 신원을 확인 애플리케이션에서 사용자가 로그인 할 때 아이디와 비밀번호를 확인하는 절차 가입 : 사용자의 아이디와 비밀번호 생성 가입한 사용자의 아이디와 비밀번호는 암호화 해서 저장 DB에 저장 로그인 시 본인의 아이디와 비밀번호 입력 비밀번호 입력 시 DB에 저장된 암호화 되어있는 비밀번호와 비교 비밀번호 일치 시 로그인 성공 로그인 성공 시 API 서버가 access token을 클라이언트에게 전송 로그인 성공 후 프론트엔드 서버에서 access token을 첨부하여 request를 서버에 전송 (매번 로그인 하지 않도록) 2. 사용자 비밀번호 암호화 암호화 하는 이유 : 외부 해킹공격, 내부의 DB 접근 시 비밀번호를 모르게 하기 위함 암호화 방법 : 단방향 해시 함수 사용 (..

    [미니터] DB연결 - 타임라인

    [미니터] DB연결 - 타임라인

    1. DB연결 함수 생성 def get_timeline(user_id): timeline = current_app.database.execute(text(""" SELECT t.user_id, t.tweet FROM tweets t LEFT JOIN users_follow_list ufl ON ufl.user_id = :user_id WHERE t.user_id = :user_id OR t.user_id = ufl.follow_user_id """), { 'user_id' : user_id }).fetchall() return [{ 'user_id' : tweet['user_id'], 'tweet' : tweet['tweet'] } for tweet in timeline] 2. HTTP 요청 http -..

    [미니터] DB연결 - 팔로우와 언팔로우

    [미니터] DB연결 - 팔로우와 언팔로우

    1. DB 연결 함수 생성 - 팔로우 def insert_follow(user_follow) : return current_app.database.execute(text(""" INSERT INTO users_follow_list( user_id, follow_user_id )VALUES ( :id, :follow ) """), user_follow).rowcount def insert_unfollow(user_unfollow) : return current_app.database.execute(text(""" DELETE FROM users_follow_list WHERE user_id = :id AND follow_user_id = :unfollow """), user_unfollow).rowcoun..

    [미니터] DB연결 - tweet 올리기

    [미니터] DB연결 - tweet 올리기

    1. DB 함수 생성 def insert_tweet(user_tweet) : return current_app.database.execute(text(""" INSERT INTO tweets ( user_id, tweet ) VALUES ( :id, :tweet ) """), user_tweet).rowcount current_app : create_app 함수에서 생성한 app 변수를 외부에서도 사용 가능하게 함 INSERT : 트윗 데이터를 tweets 테이블에 저장 rowcount : 테이블의 row 개수를 세줌 2. HTTP 요청 http -v POST 127.0.0.1:5000/tweet id=1 tweet="Hello Jarvis" DB 확인

    [미니터] DB 연결 - 회원가입

    [미니터] DB 연결 - 회원가입

    1. create_app() 함수 내에 코드 추가하여 실행 @app.route("/sign-up", methods=['GET','POST']) def sign_up(): new_user = request.json new_user_id = app.database.execute(text(""" INSERT INTO users( name, email, profile, hashed_password ) VALUES ( :name, :email, :profile, :password ) """), new_user).lastlowid row = current_app.database.execute(text(""" SELECT id, name, email, profile FROM users WHERE id = :user_..