written by 강다혜

Firebase

마이페이지에서 정보 표시를 위한 DB 연결하기

기본 정보 불러오기 - 세션 활용

마이페이지 화면에서는 로그인한 유저의 정보를 DB에서 받아와 아이디, 이메일 주소 등의 기본 정보들을 다음과 같이 표시해준다.

마이페이지 메인 정보 화면

마이페이지 메인 정보 화면

이를 구현하기 위해서 **session[’ ‘]**을 활용하는데, session에 어떤 정보를 넣어주는 지에 따라, 어떤 페이지에서든 session으로 접근하여 해당 정보를

참조할 수 있다. 예를 들어,

session['id']=id

session에 아이디 정보를 넣어줬기에, 어떤 페이지에서든 session으로 접근하여 아이디 정보의 참조가 가능하다.

다음은 **session[’ ‘]**을 활용한 마이페이지 화면의 HTML 코드이다. 볼드체로 표시한 부분을 참고하면 된다.

<table class="mypage-table">
  <tr>
    **{% if session['id'] %}**
    <td colspan="2">
      <spark class="user">**{{session['id']}}**</spark> 님 반가워요
      <button id="logout">
        <a href="/logout" style="color:white; text-decoration:none;">로그아웃</a>
      </button></td></tr>
  <tr>
    <th>정보</th>
    <td>
      <table class="profile-table">
        <tr>
          <th><img src="../static/images/userid.png"/>아이디</th>
          <td>**{{session['id']}}**</td>
        </tr>
        <tr>
          <th><img src="../static/images/email.png"/>이메일 주소</th>
          <td>
            <a href="mailto:{{ session['email'] }}" style="text-decoration:none;">**{{session['email']}}**</a>
          </td>
        </tr>
        <tr>
          <th><img src="../static/images/phone.png"/>오픈채팅방 링크</th>
          <td>
            <a href="{{session['openkakao']}}" style="text-decoration:none;">**{{session['openkakao']}}**</a>
          </td>
        </tr>
        <tr>
          <th><img src="../static/images/member.png"/>단과대학 및 학과</th>
          <td>**{{session['department']}}**</td>
        </tr>
      </table>
    </td></tr>
</table>

다음은 session에서 참조하는 유저의 정보를 DB에 저장하는 database.py 코드이다.

def insert_user(self, data, pw):
        user_info = {
            "id": data['id'],
            "pw": pw,
            # "password_check": data['password_check'],
            "email": data['email'],
            "openkakao": data['openkakao'],
            "department": data['department']
        }
        if self.user_duplicate_check(str(data['id'])):
            self.db.child("user").push(user_info)
            print(data)
            return True
        else:
            return False

DB에서 정보 불러오기

사용할 파일들