본문 바로가기

Salesforce/Development

현재 사용자 ID 정보 가져 오기(Aura, lwc, apex, vf)

728x90

현재 사용자 ID를 가져오는 방법을 알아 보겠습니다.

Apex Class
// Apex Class
public static Id getCurrentUserIdByApex() {
	return System.UserInfo.getUserId();
}

System.debug('# userId : ' + getCurrentUserIdByApex());
결과
# userId : 0056F00000A1jLGQAZ

 

Aura Component
<aura:component implements="force:appHostable,flexipage:availableForRecordHome,flexipage:availableForAllPageTypes,force:hasSObjectName,lightning:isUrlAddressable" 
                access="global">
	
    <aura:attribute name="userId" type="String"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    <div class="slds-box slds-box_small" style="background-color: white;">
        <b>Aura Component</b>
        <p>User Id : {! v.userId }</p>
    </div>

</aura:component>
({
    doInit : function(component, event, helper) {
        let userId = $A.get("$SObjectType.CurrentUser.Id");
		console.log(userId);
        component.set('v.userId', userId);
    }
})

 

LWC
<template>
    <div class="slds-box slds-box_small" style="background-color: white;">
        <b>LWC</b>
        <p>User Id : {currentUserId}</p>
    </div>
</template>
import { LightningElement } from 'lwc';
import UserId from '@salesforce/user/Id';
 
export default class CurrentUserIdLwc extends LightningElement {
    currentUserId = UserId;
}
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>54.0</apiVersion>
    <isExposed>true</isExposed>
    <masterLabel>Current User Id (LWC)</masterLabel>
    <description>Current User Id (LWC)</description>
    <targets>
        <target>lightning__RecordPage</target>
        <target>lightning__AppPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

 

Visualforce Page
<apex:page>
    <apex:slds />
    <div class="slds-scope">
        <div class="slds-box slds-box_small" style="background-color: white;">
            <b>Visualforce Page</b>
            <p>User Id : {!$User.Id}</p>
        </div>
    </div>
</apex:page>

 

적용된 모습

적용된 이미지

728x90