Face Out 4 : 로그인 & 회원가입 & 로그아웃 구현

25382 단어 Face OutFace Out

앞서 작성한 DB 연동을 DB 연동하여 Web 만들기 참고하여 바꿀 것.


이 사이트를 참고하여 진행함.

request.form 관련 에러는 이 사이트 보고 수정함.


💻 회원가입 구현

🔖 database_setup.py

  • line 42
  • class UserRegister() 추가
class User(Base):
    __tablename__ = 'user'

    id = Column(Integer, primary_key=True) # 유저 번호
    u_name = Column(String(50), nullable=False)   # 유저 이름
    u_phone = Column(String(50), nullable=False)   # 유저 핸드폰 번호
    u_id = Column(String(100), nullable=False)       # 유저 id
    u_pw = Column(String(100), nullable=False)       # 유저 pw

    @property
    def serialize(self):
        return {
            'id': self.id,
            'u_name': self.u_nameame,
            'u_phone': self.u_phone,
            'u_id': self.u_id,
            'u_pw': self.u_pw,
        }

🔖 init.py

  • def sign_up() 변경
  • line 5, 60
  • id가 primary key(?) 였는데 u_id 값이 들어가서 오류가 난 듯!
# line 5
from database_setup import Base, Question, QuestionContent, UserRegister

# line 60
# 회원가입
@app.route('/signup', methods=['GET', 'POST'])
def sign_up():
    if request.method == 'GET':
        return render_template('/user_templates/signup.html')
    else:
        u_name = request.form.get('u_name', False)
        u_phone = request.form.get('u_phone', False)
        u_id = request.form.get('u_id', False)
        u_pw = request.form.get('u_pw', False)

        if not (u_name and u_phone and u_id and u_pw):
            # 하나라도 작성하지 않으면 다시 회원가입 화면
            return render_template('/user_templates/signup.html')
        elif db_session.query(User).filter_by(u_id=u_id).first() is not None:  # ID 조회해서 존재하는 아이디인지 확인
            flash('사용할 수 없는 아이디입니다.')
            return redirect('/signup')
        else:
            userinfo = User()
            userinfo.u_name = u_name
            userinfo.u_phone = u_phone
            userinfo.u_id = u_id
            userinfo.u_pw = u_pw
            db_session.add(userinfo)
            db_session.commit()

            session['u_id'] = u_id
            session['logged_in'] = True

            # 모두 작성하면 login 화면
            return render_template('/user_templates/main.html', logged_in=True)
        return redirect('/')

🔖 signup.html

  • line 59 - 75
<form method="post" id="contactForm"  novalidate >
                        <div class="form-group floating-label-form-group controls">
                            <input class="form-control" name="u_name" placeholder="Name" required data-validation-required-message="Please enter Your Name." style="font-size:25px; height:80px;"/>
                        </div>
                        <div class="form-group floating-label-form-group controls" >
                            <input class="form-control" name="u_phone" placeholder="Phone Number" required data-validation-required-message="Please enter Your Phone Number." style="font-size:25px; height:80px;"/>
                        </div>
                        <div class="form-group floating-label-form-group controls" >
                            <input class="form-control" name="u_id" placeholder="ID" required data-validation-required-message="Please enter your ID." style="font-size:25px; height:80px;"/>
                        </div>
                        <div class="form-group floating-label-form-group controls" >
                            <input class="form-control" name="u_pw" placeholder="PW" required data-validation-required-message="Please enter your PW." style="font-size:25px; height:80px;"/>
                        </div>
                        <br/><br/>
                        <button class="btn btn-secondary" type="submit" style="height:50px; text-align:center; line-height:10px; font-size:20px;">
                    회원가입</button>
                    </form>



💻 로그인 구현

🔖 login.html

  • line 59 - 72
<form method="post" id="contactForm"  novalidate >
                        <div class="form-group floating-label-form-group controls" >
                            <input class="form-control" name="u_id" placeholder="ID" required data-validation-required-message="Please enter your ID." style="font-size:25px; height:80px;"/>
                        </div>
                        <div class="form-group floating-label-form-group controls" >
                            <input class="form-control" name="u_pw"  placeholder="PW" required data-validation-required-message="Please enter your PW." style="font-size:25px; height:80px;"/>
                        </div>
                        <br/><br/>
                        <button class="btn btn-secondary" type="submit" style="height:50px; width:150px; text-align:center; line-height:10px; font-size:20px;" >
                    로그인</button>
                    <br><br>
                        <button class="btn btn-secondary" type="button" style="height:50px; width:150px; text-align:center; line-height:10px; font-size:20px;" onClick="location.href='/signup'">
                    회원가입</button>
                    </form>

🔖 init.py

  • line 88 - 105
# 로그인 구현
@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('/user_templates/login.html')
    else:
        u_id = request.form['u_id']
        u_pw = request.form['u_pw']
        data = db_session.query(User).filter_by(u_id=u_id, u_pw=u_pw).first()  # ID/PW 조회Query 실행
        if data is not None:  # 쿼리 데이터가 존재하면
            session['u_id'] = u_id
            session['logged_in'] = True
            return render_template('/user_templates/main.html', logged_in=True)  # 쿼리 데이터가 있으면 main으로
        else:
            flash('잘못 입력하셨습니다. 다시 로그인해주세요.')
            return redirect('/login')



💻 로그아웃 구현

🔖 init.py

  • line 162 - 166
# 로그아웃 구현
@app.route('/logout', methods=['GET','POST'])
def logout():
    session['logged_in'] = False
    session.clear()
    return render_template('/user_templates/main.html',logged_in=False)

좋은 웹페이지 즐겨찾기